Linux多线程-线程同步
线程同步
1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 static int val = 0; 8 void* threadEntry(void* arg) 9 { 10 for(int i = 0; i<1000000; ++i) 11 { 12 val = val + 1; 13 } 14 return NULL; 15 } 16 17 int main(int argc, char** argv) 18 { 19 pthread_t th1,th2; 20 pthread_create(&th1, NULL, threadEntry, NULL); 21 pthread_create(&th2, NULL, threadEntry, NULL); 22 pthread_join(th1, NULL); 23 pthread_join(th2, NULL); 24 printf("calc result : %d\n", val); 25 return 0; 26 }
calc result : 1013285
typedef struct { // 互斥量的类型属性 // PTHREAD_MUTEX_NORMAL - 正常互斥量 // PTHREAD_MUTEX_ERRORCHECK - 错误检查互斥量 // PTHREAD_MUTEX_RECURSIVE - 递归互斥量 // PTHREAD_MUTEX_DEFAULT - 默认互斥量类型 int type; // 保留字段 long int __reserved; // 锁的拥有者线程ID __pthread_mutex_slist_t *owner; // 等待该互斥量的线程列表 __pthread_mutex_slist_t __list; } pthread_mutex_t;
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t* attr);
参数说明
返回值
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
参数说明
返回值
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
参数说明
返回值
#include <pthread.h>
int pthread_mutex_trylock(pthread_mutex_t *mutex);
参数说明
返回值
#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
参数说明
返回值
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
参数说明
返回值
#include <pthread.h>
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
参数说明
返回值
#include <pthread.h> int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
参数说明
返回值
#include <pthread.h> int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
参数说明
返回值
#include <pthread.h> int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
参数说明
返回值
#include <pthread.h> int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared);
参数说明
返回值
1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 static pthread_mutex_t mutex; 8 9 void func() 10 { 11 static int i = 0; 12 if(i >= 10) return; 13 pthread_mutex_lock(&mutex); 14 printf("func : %d\n", i++); 15 func(); 16 pthread_mutex_unlock(&mutex); 17 } 18 19 void* threadEntry(void* arg) 20 { 21 func(); 22 return NULL; 23 } 24 25 int main(int argc, char** argv) 26 { 27 pthread_t th; 28 pthread_mutexattr_t muattr; 29 pthread_mutexattr_init(&muattr); 30 pthread_mutexattr_settype(&muattr, PTHREAD_MUTEX_RECURSIVE); 31 pthread_mutex_init(&mutex, &muattr); 32 33 pthread_create(&th, NULL, threadEntry, NULL); 34 pthread_join(th, NULL); 35 36 pthread_mutexattr_destroy(&muattr); 37 pthread_mutex_destroy(&mutex); 38 return 0; 39 }
func : 0 func : 1 ... func : 8 func : 9
#include <pthread.h> int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
参数说明
返回值
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
#include <pthread.h>
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h>
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h>
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h>
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h>
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
参数说明
返回值
#include <pthread.h> //初始化 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr); //销毁 int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
#include <pthread.h> //获取共享属性 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared); //设置共享属性 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
#include <pthread.h> //设置读写偏好 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref); //获取读写偏好 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref);
#include <pthread.h> int pthread_cond_init(pthread_cond_t * cond, const pthread_condattr_t* attr);
参数说明
返回值
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
#include <pthread.h>
int pthread_cond_destroy(pthread_cond_t *cond);
参数说明
返回值
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
参数说明
返回值
注意事项
#include <pthread.h> #include <time.h> struct timespec { time_t tv_sec; // 秒数 long tv_nsec; // 纳秒数 }; int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
参数说明
返回值
注意事项
用法
1 void *threadEntry(void *arg) 2 { 3 struct timespec timeout; 4 clock_gettime(CLOCK_REALTIME, &timeout); 5 timeout.tv_sec += 3; // 延时3s 6 pthread_mutex_lock(&mutex); 7 int ret = pthread_cond_timedwait(&cond, &mutex, &timeout); 8 if (ret == 0) 9 { 10 // 满足条件 11 } 12 else if (ret == ETIMEDOUT) 13 { 14 // 超时 15 } 16 else 17 { 18 // 其他错误 19 } 20 return NULL; 21 }
#include <pthread.h> //唤醒所有等待的线程 int pthread_cond_broadcast(pthread_cond_t* cond); //唤醒所有等待的线程中的一个 int pthread_cond_signal(pthread_cond_t* cond);
参数说明
返回值
#include <pthread.h> int pthread_condattr_init(pthread_condattr_t *attr); int pthread_condattr_destroy(pthread_condattr_t *attr);
#include <pthread.h> //设置时钟类型 int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id); //获取时钟类型 int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id);
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <pthread.h> 4 #include <time.h> 5 #include <unistd.h> 6 7 static pthread_mutex_t mutex; 8 static pthread_cond_t cond; 9 static int condVal = 0; 10 11 void *threadComsumer(void *arg) 12 { 13 char name = (char)arg; 14 printf("comsumer %c start work\n", name); 15 while (1) 16 { 17 pthread_mutex_lock(&mutex); 18 while (condVal <= 0) 19 { 20 pthread_cond_wait(&cond, &mutex); 21 } 22 int count = (rand() % 3) + 1; 23 if(count > condVal) 24 count = condVal; 25 condVal -= count; 26 printf("comsumer %c get %d stuff, cur total stuff : %d\n", name, count, condVal); 27 pthread_mutex_unlock(&mutex); 28 usleep(500 * 1000); //500ms 29 } 30 return NULL; 31 } 32 33 void *threadproductor(void *arg) 34 { 35 printf("productor start work\n"); 36 while (1) 37 { 38 pthread_mutex_lock(&mutex); 39 int count = (rand() % 5) + 1; 40 condVal += count; 41 printf("produce %d stuff\n", count); 42 pthread_mutex_unlock(&mutex); 43 if (count > 0) 44 pthread_cond_broadcast(&cond); 45 sleep(2); 46 } 47 return 0; 48 } 49 50 int main(int argc, char **argv) 51 { 52 srand(time(0)); 53 pthread_t th_arr_comsumer[5]; 54 pthread_t th_productor; 55 pthread_mutex_init(&mutex, NULL); 56 pthread_cond_init(&cond, NULL); 57 pthread_create(&th_productor, NULL, threadproductor, NULL); 58 59 for (int i = 0; i < 5; ++i) 60 pthread_create(&th_arr_comsumer[i], NULL, threadComsumer, 'A' + i); 61 62 pthread_join(th_productor, NULL); 63 for (int i = 0; i < 5; ++i) 64 pthread_join(th_arr_comsumer[i], NULL); 65 66 pthread_cond_destroy(&cond); 67 pthread_mutex_destroy(&mutex); 68 return 0; 69 }
producer start work produce 4 stuff comsumer A start work comsumer A get 2 stuff, cur total stuff : 2 comsumer B start work comsumer B get 2 stuff, cur total stuff : 0 comsumer C start work comsumer D start work comsumer E start work produce 5 stuff comsumer D get 1 stuff, cur total stuff : 4 comsumer E get 2 stuff, cur total stuff : 2 comsumer A get 2 stuff, cur total stuff : 0 produce 4 stuff comsumer C get 3 stuff, cur total stuff : 1 comsumer B get 1 stuff, cur total stuff : 0 produce 5 stuff comsumer D get 1 stuff, cur total stuff : 4 comsumer A get 1 stuff, cur total stuff : 3 comsumer B get 2 stuff, cur total stuff : 1 comsumer E get 1 stuff, cur total stuff : 0 ...