天天看点

unix环境高级编程(阅读笔记)----线程同步

多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图。

1.互斥量

可以通过使用pthread的互斥接口保护数据,确保同一时间只有一个线程访问数据,互斥量(mutex)从本质上说是一把 锁,在访问共享资源前对互斥量进行枷锁,在访问完成后释放互斥量上的锁。对互斥量进行加锁后,任何其他试图再次对互斥 量进行加锁的线程将被阻塞知道当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上阻塞线程都会 变成可运行状态,第一个变为运行状态的线程可以对互斥量进行加锁,其他线程将会看到互斥锁依然被锁住,只有回去再次等 待它重新变成可用。在这种方式下,每次只有一个线程可以向前执行。 互斥变量用pthread_mutex_t数据类型来表示,在使用互斥变量以前,必须首先对它进行初始化,可以把它置为常量 PTHREAD_MUTEX_INITIALIZER(只对静态分配的互斥量),也可以通过调用pthread_mutex_init函数进行初始化。如果动态地 分配互斥量(例如通过调用malloc),那么在释放内存前需要调用pthread_mutex_destroy。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include<pthread.h>  
  2. int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);  
  3. int pthread_mutex_destroy(pthread_mutex_t * mutex);  
  4. //成功返回0,否则返回错误编号  

使用默认的属性初始化互斥量,只要将attr设置为NULL。

对互斥量进行加锁,需要调用pthread_mutex_lock,如果互斥量已经上锁,调用线程将阻塞直到互斥量被解锁。对互斥量 解锁,需要调用pthread_mutex_unlock。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <pthread.h>  
  2. int pthread_mutex_lock(pthread_mutex_t *mutex);  
  3. int pthread_mutex_trylock(pthread_mutex)_t *mutex);  
  4. int pthread_mutex_unlock(pthread_mutex_t *mutex);  
  5. //如果成功返回0,否则返回错误编号。  

如果线程不希望被阻塞,它可以使用pthread_mutex_trylock尝试对互斥量进行加锁。如果调用pthread_mutex_trylock时 互斥量处于未锁住状态,那么pthread_mutex_trylock将锁住互斥量,不会出现并返回0,否则pthread_mutex_trylock就会失败, 不能锁住互斥量,而返回EBUSY。

实践: 在没有使用互斥锁时:

[cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <malloc.h>  
  4. #include <string.h>  
  5. void* th_func(void* arg){  
  6.         int i;  
  7.         for(i=0; i<5; i++){  
  8.                 printf("1\n");  
  9.                 sleep(1);  
  10.                 printf("2\n");  
  11.         }  
  12. }  
  13. int main(void){  
  14.         int ret;  
  15.         pthread_t tid1,tid2;  
  16.         ret = pthread_create(&tid1, NULL, th_func, NULL);  
  17.         if(ret != 0){  
  18.                 printf("pthread_create:%s\n",strerror(ret));  
  19.                 return -1;  
  20.         }  
  21.         ret = pthread_detach(tid1);  
  22.         if(ret != 0){  
  23.                 printf("pthread_detach:%s\n",strerror(ret));  
  24.                 return -1;  
  25.         }  
  26.         ret = pthread_create(&tid2, NULL, th_func, NULL);  
  27.         if(ret != 0){  
  28.                 printf("pthread_create:%s\n",strerror(ret));  
  29.                 return -1;  
  30.         }  
  31.         ret = pthread_detach(tid2);  
  32.         if(ret != 0){  
  33.                 printf("pthread_detach:%s\n",strerror(ret));  
  34.                 return -1;  
  35.         }  
  36.         sleep(15);  
  37.         return 0;  
  38. }  

程序输出为: 1

1

2

1

2

1

2

1

2

1

2

1

2

1

2

1

2

1

2

2

使用了互斥锁之后: [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <malloc.h>  
  4. #include <string.h>  
  5. pthread_mutex_t *mutex;  
  6. void* th_func(void* arg){  
  7.         int i;  
  8.         for(i=0; i<5; i++){  
  9.                 pthread_mutex_lock(mutex);  
  10.                 printf("1\n");  
  11.                 sleep(1);  
  12.                 printf("2\n");  
  13.                 pthread_mutex_unlock(mutex);  
  14.         }  
  15. }  
  16. int main(void){  
  17.         int ret,result = 0;  
  18.         pthread_t tid1,tid2;  
  19.         mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));  
  20.         if(mutex == NULL){  
  21.                 perror("malloc");  
  22.                 result = -1;  
  23.                 goto FINALLY;  
  24.         }  
  25.         pthread_mutex_init(mutex,NULL);  
  26.         ret = pthread_create(&tid1, NULL, th_func, NULL);  
  27.         if(ret != 0){  
  28.                 printf("pthread_create:%s\n",strerror(ret));  
  29.                 result = -1;  
  30.                 goto FINALLY;  
  31.         }  
  32.         ret = pthread_detach(tid1);  
  33.         if(ret != 0){  
  34.                 printf("pthread_detach:%s\n",strerror(ret));  
  35.                 result = -1;  
  36.                 goto FINALLY;  
  37.         }  
  38.         ret = pthread_create(&tid2, NULL, th_func, NULL);  
  39.         if(ret != 0){  
  40.                 printf("pthread_create:%s\n",strerror(ret));  
  41.                 result = -1;  
  42.                 goto FINALLY;  
  43.         }  
  44.         ret = pthread_detach(tid2);  
  45.         if(ret != 0){  
  46.                 printf("pthread_detach:%s\n",strerror(ret));  
  47.                 result = -1;  
  48.                 goto FINALLY;  
  49.         }  
  50.         sleep(15);  
  51. FINALLY:  
  52.         if(mutex != NULL){  
  53.                 pthread_mutex_destroy(mutex);  
  54.                 free(mutex);  
  55.         }  
  56.         return result;  
  57. }  

程序输出为: 1

2

1

2

1

2

1

2

1

2

1

2

1

2

1

2

1

2

1

2

2.避免死锁

如果线程试图对同一个互斥量加锁两次,那么它自身就会陷入死锁状态,使用互斥量时,还有其他更不明显的方式也能产生死锁, 例如,程序中使用多个互斥量,如果允许一个线程一直占有一个互斥量,并且试图锁住第二个互斥量时处于阻塞状态,但是拥有第二 个互斥量的线程也在试图锁住第一个互斥量,这时就会发生死锁。因为两个线程都在相互请求另一个线程拥有的资源,所以这两个线程 都无法向前运行,于是就产生死锁。 可以通过控制互斥量加锁的顺序来避免死锁的发生。例如,假设需要对两个互斥量A和B同时加锁,如果所有线程总是在对互斥量B 加锁之前锁住A,那么使用这两个互斥量不会产生死锁,类似地,如果所有的此案成总是在锁住互斥量A之前锁住B,那么也不会发生死 锁。

3.读写锁

读写锁与互斥量类似,不过读写锁允许更高的并行性。互斥量要么是锁住状态要么不加锁状态,而且一次只有一个线程可以对其 加锁。读写锁可以由三种状态:读模式下加锁状态,写模式下加锁状态,不加锁状态。一次只有一个线程可以占有写模式的读写锁, 但是多个线程可以同时占有读模式的读写锁。 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞。当读写锁在读加锁状态时,所有试图 以读模式对它进行加锁的线程都可以得到访问权,但是如果线程希望以写模式对此锁进行加锁,他必须阻塞直到所有的线程释放读锁。 当读写锁处理读模式锁住状态时,如果有另外的线程试图以写模式加锁,读写锁通常会阻塞随后的读模式锁请求,这样可以避免读模式 锁长期占用,而等待的些模式请求一直得不到满足。 读写锁非常适用于对数据结构读的次数远大于写的情况。 通过调用pthread_rwlock_init进程初始化,如果希望读写锁有默认的属性,可以传一个空指针给attr。 在释放读写锁占用的内存之前,需要调用pthread_rwlock_destroy做清理工作。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <pthread.h>  
  2. int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);  
  3. int pthread_rwlock_destory(pthread_rwlock_t *rwlock);  
  4. //若成功返回0,否则返回错误编号。  

在读模式下锁住读写锁,需要调用pthread_rwlock_rdlock,要在写模式下锁定读写锁,需要调用pthread_rwlock_wrlock,不管以 何种方式锁住读写锁,都可以调用pthread_rwlock_unlock进行解锁。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include<pthread.h>  
  2. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);  
  3. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);  
  4. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);  
  5. //成功则返回0,否则返回错误编号。  

Single UNIX Specification同样定义了有条件的读写锁原语版本。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <pthread.h>  
  2. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);  
  3. int pthread_rwlock_tryrwlock(pthread_rwlock_t *rwlock);  
  4. //成功返回0,否则返回错误编号。  

实践: [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <malloc.h>  
  4. #include <string.h>  
  5. pthread_rwlock_t *rwlock;  
  6. int gi=0;  
  7. void* th_func1(void* arg){  
  8.         int i;  
  9.         for(i=0; i<5; i++){  
  10.                 pthread_rwlock_rdlock(rwlock);  
  11.                 printf("start1:%d\n",gi);  
  12.                 sleep(1);  
  13.                 printf("end1:%d\n",gi);  
  14.                 pthread_rwlock_unlock(rwlock);  
  15.                 sleep(1);  
  16.         }  
  17. }  
  18. void* th_func2(void* arg){  
  19.         int i;  
  20.         for(i=0; i<5; i++){  
  21.                 pthread_rwlock_rdlock(rwlock);  
  22.                 printf("start2:%d\n",gi);  
  23.                 sleep(1);  
  24.                 printf("end2:%d\n",gi);  
  25.                 pthread_rwlock_unlock(rwlock);  
  26.                 sleep(1);  
  27.         }  
  28. }  
  29. void* th_func3(void* arg){  
  30.         int i;  
  31.         for(i=0; i<5; i++){  
  32.                 pthread_rwlock_wrlock(rwlock);  
  33.                 gi++;  
  34.                 sleep(1);  
  35.                 pthread_rwlock_unlock(rwlock);  
  36.                 sleep(1);  
  37.         }  
  38. }  
  39. int main(void){  
  40.         int ret,result = 0;  
  41.         pthread_t tid1,tid2,tid3;  
  42.         rwlock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t));  
  43.         if(rwlock == NULL){  
  44.                 perror("malloc");  
  45.                 goto FINALLY;  
  46.         }  
  47.         ret = pthread_create(&tid1, NULL, th_func1, NULL);  
  48.         if(ret != 0){  
  49.                 printf("pthread_create:%s\n",strerror(ret));  
  50.                 result = -1;  
  51.                 goto FINALLY;  
  52.         }  
  53.         ret = pthread_detach(tid1);  
  54.         if(ret != 0){  
  55.                 printf("pthread_detach:%s\n",strerror(ret));  
  56.                 result = -1;  
  57.                 goto FINALLY;  
  58.         }  
  59.         ret = pthread_create(&tid2, NULL, th_func2, NULL);  
  60.         if(ret != 0){  
  61.                 printf("pthread_create:%s\n",strerror(ret));  
  62.                 result = -1;  
  63.                 goto FINALLY;  
  64.         }  
  65.         ret = pthread_detach(tid2);  
  66.         if(ret != 0){  
  67.                 printf("pthread_detach:%s\n",strerror(ret));  
  68.                 result = -1;  
  69.                 goto FINALLY;  
  70.         }  
  71.         ret = pthread_create(&tid3, NULL, th_func3, NULL);  
  72.         if(ret != 0){  
  73.                 printf("pthread_create:%s\n",strerror(ret));  
  74.                 result = -1;  
  75.                 goto FINALLY;  
  76.         }  
  77.         ret = pthread_detach(tid3);  
  78.         if(ret != 0){  
  79.                 printf("pthread_detach:%s\n",strerror(ret));  
  80.                 result = -1;  
  81.                 goto FINALLY;  
  82.         }  
  83.         sleep(15);  
  84. FINALLY:  
  85.         if(rwlock != NULL){  
  86.                 pthread_rwlock_destroy(rwlock);  
  87.                 free(rwlock);  
  88.         }  
  89.         return result;  
  90. }  

结果: start2:1

start1:1

end2:1

end1:1

start2:2

start1:2

end2:2

end1:2

start2:3

start1:3

end2:3

end1:3

start2:4

start1:4

end2:4

end1:4

start2:5

start1:5

end2:5

end1:5

一次循环中start和end中的值都是一样的。

如果我们修改下程序: [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <malloc.h>  
  4. #include <string.h>  
  5. int gi=0;  
  6. void* th_func1(void* arg){  
  7.         int i;  
  8.         for(i=0; i<5; i++){  
  9.                 printf("start1:%d\n",gi);  
  10.                 sleep(1);  
  11.                 printf("end1:%d\n",gi);  
  12.                 sleep(1);  
  13.         }  
  14. }  
  15. void* th_func2(void* arg){  
  16.         int i;  
  17.         for(i=0; i<5; i++){  
  18.                 printf("start2:%d\n",gi);  
  19.                 sleep(1);  
  20.                 printf("end2:%d\n",gi);  
  21.                 sleep(1);  
  22.         }  
  23. }  
  24. void* th_func3(void* arg){  
  25.         int i;  
  26.         for(i=0; i<5; i++){  
  27.                 gi++;  
  28.                 sleep(1);  
  29.         }  
  30. }  
  31. int main(void){  
  32.         int ret,result = 0;  
  33.         pthread_t tid1,tid2,tid3;  
  34.         ret = pthread_create(&tid1, NULL, th_func1, NULL);  
  35.         if(ret != 0){  
  36.                 printf("pthread_create:%s\n",strerror(ret));  
  37.                 result = -1;  
  38.                 goto FINALLY;  
  39.         }  
  40.         ret = pthread_detach(tid1);  
  41.         if(ret != 0){  
  42.                 printf("pthread_detach:%s\n",strerror(ret));  
  43.                 result = -1;  
  44.                 goto FINALLY;  
  45.         }  
  46.         ret = pthread_create(&tid2, NULL, th_func2, NULL);  
  47.         if(ret != 0){  
  48.                 printf("pthread_create:%s\n",strerror(ret));  
  49.                 result = -1;  
  50.                 goto FINALLY;  
  51.         }  
  52.         ret = pthread_detach(tid2);  
  53.         if(ret != 0){  
  54.                 printf("pthread_detach:%s\n",strerror(ret));  
  55.                 result = -1;  
  56.                 goto FINALLY;  
  57.         }  
  58.         ret = pthread_create(&tid3, NULL, th_func3, NULL);  
  59.         if(ret != 0){  
  60.                 printf("pthread_create:%s\n",strerror(ret));  
  61.                 result = -1;  
  62.                 goto FINALLY;  
  63.         }  
  64.         ret = pthread_detach(tid3);  
  65.         if(ret != 0){  
  66.                 printf("pthread_detach:%s\n",strerror(ret));  
  67.                 result = -1;  
  68.                 goto FINALLY;  
  69.         }  
  70.         sleep(15);  
  71. FINALLY:  
  72.         return result;  
  73. }  

运行结果: start2:1

start1:1

end2:2

end1:2

start2:3

start1:3

end2:4

end1:4

start2:5

start1:5

end2:5

end1:5

start2:5

start1:5

end2:5

end1:5

start2:5

start1:5

end2:5

end1:5

一次循环中start和end中的值可能会不一样。

4.条件变量

条件变量是线程可用的另一种同步机制。条件变量给多个线程提供了一个回合的场所。条件变量与互斥量一起使用时,允许线程 以无竞争的方式等待特定的条件发生。 条件本身是由互斥量保护的,线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会觉察到这种改变,因为 必须锁住互斥量后才能计算条件。 条件变量使用之前必须先进行初始化,pthread_cond_t数据结构代表的条件变量可以用两种方式进行初始化,可以把常量 PTHREAD_COND_INITIALIZER赋给静态分配的条件变量,但是如果条件变量是动态分配的,可以使用pthread_cond_init函数初始化。 在释放底层的内存空间之前,可以使用pthread_cond_destroy函数对条件变量进行去除初始化。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include<pthread.h>  
  2. int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t *restrict attr);  
  3. int pthread_cond_destroy(pthread_cond_t *cond);  
  4. //成功则返回0,否则返回错误编号。  

使用pthread_cond_wait等待条件变成真。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include<pthread.h>  
  2. int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);  
  3. //成功返回0,否则返回错误编号。  

传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线程列表上, 然后对互斥量解锁,这两个操作时原子操作,这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道, 这样线程就不会错过条件的任何变化,pthread_cond_wait返回时,互斥量再次被锁住。

[cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include<pthread.h>  
  2. int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex,  
  3. const struct timespec *restrict timeout);  
  4. //成功返回0,否则返回错误编号。  

pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数相似,只是多了一个timeout,timeout值指定了等待的时间。它 通过timespec结构指定。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. struct timespec{  
  2.     time_t tv_sec; //秒数  
  3.     long tv_nsec; //纳秒  
  4. }  

使用这个结构体时,需要指定等待多长时间,时间值是一个绝对值而不是相对值,例如如果要等待3分钟,就需要把当前时间加上3分钟 再转换到timespec结构。 如果时间到了但是条件还是没有出现,pthread_cond_timedwait将重新获取互斥量然后返回错误ETIMEDOUT。 从pthread_cond_wait或者pthread_cond_timedwait调用成功返回时,线程需要重新计算条件,因为其他的线程可能已经在运行并改变 了条件。

有两个函数可以用于通知线程条件已经满足。pthread_cond_signal函数将唤醒等待该线程的某个线程,而pthread_cond_broadcast函数 将唤醒等待该条件的所有线程。 [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <pthread.h>  
  2. int pthread_cond_siganl(pthread_cond_t *cond);  
  3. int pthread_cond_broadcast(pthread_cond_t *cond);  
  4. //成功则返回0,否则返回错误编号。  

实践: [cpp]  view plain  copy  

unix环境高级编程(阅读笔记)----线程同步
unix环境高级编程(阅读笔记)----线程同步
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <string.h>  
  4. pthread_cond_t qready = PTHREAD_COND_INITIALIZER;  
  5. pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;  
  6. int gi=0;  
  7. void* substract(void* arg){  
  8.         int i;  
  9.         for(i=0; i<5; i++){  
  10.                 pthread_mutex_lock(&qlock);  
  11.                 while(gi == 0)  
  12.                         pthread_cond_wait(&qready, &qlock);  
  13.                 printf("before substract gi:%d.\n",gi);  
  14.                 gi--;  
  15.                 printf("after substract gi:%d.\n",gi);  
  16.                 pthread_mutex_unlock(&qlock);  
  17.         }  
  18. }  
  19. void* add(void* arg){  
  20.         int i;  
  21.         for(i=0; i<5; i++){  
  22.                 pthread_mutex_lock(&qlock);  
  23.                 printf("before add gi:%d.\n",gi);  
  24.                 gi++;  
  25.                 printf("after add gi:%d.\n",gi);  
  26.                 pthread_mutex_unlock(&qlock);  
  27.                 pthread_cond_signal(&qready);  
  28.         }  
  29. }  
  30. int main(void){  
  31.         int ret;  
  32.         pthread_t tid1,tid2;  
  33.         ret = pthread_create(&tid1, NULL, substract, NULL);  
  34.         if(ret != 0){  
  35.                 printf("pthread_create:%s",strerror(ret));  
  36.                 return -1;  
  37.         }  
  38. <span style="white-space:pre">  </span>sleep(1);   
  39.         ret = pthread_create(&tid2, NULL, add, NULL);  
  40.         if(ret != 0){  
  41.                 printf("pthread_create:%s",strerror(ret));  
  42.                 return -1;  
  43.         }  
  44.         ret = pthread_join(tid1,NULL);  
  45.         if(ret != 0){  
  46.                 printf("pthread_join:%s",strerror(ret));  
  47.                 return -1;  
  48.         }  
  49.         ret = pthread_join(tid2,NULL);  
  50.         if(ret != 0){  
  51.                 printf("pthread_join:%s",strerror(ret));  
  52.                 return -1;  
  53.         }  
  54.         return 0;  
  55. }  

运行结果: before add gi:0.

after add gi:1.

before substract gi:1.

after substract gi:0.

before add gi:0.

after add gi:1.

before substract gi:1.

after substract gi:0.

before add gi:0.

after add gi:1.

before substract gi:1.

after substract gi:0.

before add gi:0.

after add gi:1.

before substract gi:1.

after substract gi:0.

before add gi:0.

after add gi:1.

before substract gi:1.

after substract gi:0.

自旋锁

  自旋锁与互斥量类似,但它不是通过休眠使进程阻塞,而是在获取锁之前一直处于忙等(自旋)状态。自旋锁可用于以下情况:锁被持有的时间短,而且线程并不希望在重新调度上花费太多的成本。

  自旋锁通常作为底层原语用于实现其他类型的锁。根据它们所基于的系统体系结构,可以通过使用测试并设置指令有效地实现。当然这里说的有效也还是会导致CPU资源的浪费:当线程自旋等待锁变为可用时,CPU不能做其他的事情。这也是自旋锁只能够被位置一小段时间的原因。

  自旋锁用在非抢占式内核中时是非常有用的:除了提供互斥机制以外,它们会阻塞中断,这样中断处理程序就不会让系统陷入死锁状态,因为它需要获取已被加锁的自旋锁(把中断想成另一种抢占)。在这种类型的内核中,中断处理程序不能休眠,因为它们能用的同步原语只能是自旋锁。

屏障

  屏障(barrier)是用户协调多个线程并行工作的同步机制。屏障允许每个线程等待,直到所有的合作线程都到达某一点,然后从该点继续执行。我们已经看到一种屏障,pthread_join函数是一种屏障,允许一个线程等待,直到另一个线程退出。

  但是屏障对象的概念更广,它们允许任意数量的线程等待,直到所有的线程处理完工作,而线程不需要退出。所有线程到达屏障后可以直接工作。

互斥锁和自旋锁的区别:

  自旋锁是一种非阻塞锁,也就是说,如果某线程需要获取自旋锁,但该锁已经被其他线程占用时,该线程不会被挂起,而是在不断的消耗CPU的时间,不停的试图获取自旋锁。

  互斥量是阻塞锁,当某线程无法获取互斥量时,该线程会被直接挂起,该线程不再消耗CPU时间,当其他线程释放互斥量后,操作系统会激活那个被挂起的线程,让其投入运行。

  两种锁适用于不同场景:

  如果是多核处理器,如果预计线程等待锁的时间很短,短到比线程两次上下文切换时间要少的情况下,使用自旋锁是划算的。

  如果是多核处理器,如果预计线程等待锁的时间较长,至少比两次线程上下文切换的时间要长,建议使用互斥量。

  如果是单核处理器,一般建议不要使用自旋锁。因为,在同一时间只有一个线程是处在运行状态,那如果运行线程发现无法获取锁,只能等待解锁,但因为自 身不挂起,所以那个获取到锁的线程没有办法进入运行状态,只能等到运行线程把操作系统分给它的时间片用完,才能有机会被调度。这种情况下使用自旋锁的代价 很高。

  如果加锁的代码经常被调用,但竞争情况很少发生时,应该优先考虑使用自旋锁,自旋锁的开销比较小,互斥量的开销较大。

继续阅读