#include<pthread.h>
linux 多线程编程:
pthread_t 线程名
pthread_create(pthread * thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);) 创建线程
pthread_exit(void *retval) 结束线程 retval存放线程退出状态
pthread_join(pthread_t thread, void** retval) 等待线程
//再就是 信号量semaphore 和 互斥锁mutex_lock
pthread_create(&keyThr, NULL, keyThrFxn, "Key ")其中keyThrFxn 为线程函数
互斥锁:保护同一线程的资源同一时刻只能单一线程访问
初始化:静态初始化 pthread_mutex_t MUTEX=PTHREAD_MUTEX_INITIALIZER 互斥锁名
动态初始化 int pthread_mutex_init (pthread_mutex_t *__mutex,__const pthread_mutexattr_t *__mutexattr);
pthread_mutex_lock(pthread_mutex_t *__mutex);加锁 //加锁成功返回0,失败返回错误编号
pthread_mutex_unlock(pthread_mutex_t *__mutex);解锁
pthread_mutex_destroy(pthread_mutex_t *__mutex);销毁
读写锁:int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,__const pthread_rwlockattr_t *__restrict__attr);
int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);//读加锁
int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);//写加锁
int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);//读写
原文地址:https://www.cnblogs.com/bingoc/p/10737544.html