方法概览

Image 37.png

互斥量

原子性

◆ 原子性是指一系列操作不可被中断的特性
◆ 这一系列操作要么全部执行完成,要么全部没有执行
◆ 不存在部分执行部分未执行的情况

概述

互斥量可以保证两个线程指令的先后执行**,保证关键操作的原子性,通俗的来说就是一串指令执行过程中,不让别的指令插入执行,这一串指令执行完,下一串指令才能执行。
两个进程访问临界资源,保证对临界资源的访问是线性的
Image 38.png
◆ 互斥量是最简单的线程同步的方法
◆ 互斥量(互斥锁),处于两态之一的变量:解锁和加锁
◆** 一个资源加锁了,就表示这个资源被使用着,另外一个进程想使用,只能等待正在使用的进程解锁
◆ 两个状态可以保证资源访问的串行

实际操作

◆ 操作系统直接提供了互斥量的API
◆ 开发者可以直接使用API完成资源的加锁、解锁操作
◆ pthread_mutex_t
◆ pthread_mutex_lock
◆ pthread_mutex_unlock

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <vector>
  6. //定义互斥量
  7. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  8. // 临界资源
  9. int num = 0;
  10. // 生产者
  11. void *producer(void*){
  12. int times = 100000000;
  13. while(times --){
  14. //加锁
  15. pthread_mutex_lock(&mutex);
  16. num += 1;
  17. //解锁
  18. pthread_mutex_unlock(&mutex);
  19. }
  20. }
  21. // 消费者
  22. void *comsumer(void*){
  23. int times = 100000000;
  24. while(times --){
  25. pthread_mutex_lock(&mutex);
  26. num -= 1;
  27. pthread_mutex_unlock(&mutex);
  28. }
  29. }
  30. int main(){
  31. printf("Start in main function.");
  32. pthread_t thread1, thread2;
  33. pthread_create(&thread1, NULL, &producer, NULL);
  34. pthread_create(&thread2, NULL, &comsumer, NULL);
  35. pthread_join(thread1, NULL);
  36. pthread_join(thread2, NULL);
  37. printf("Print in main function: num = %d\n", num);
  38. return 0;
  39. }

自旋锁

概述

自旋锁的原理和互斥锁原理是相似的,也是对临界资源加锁解锁,不同的是使用自旋锁,会一直拿着cpu,一直处于工作状态不停的问锁可不可以用
◆ 自旋锁也是一种多线程同步的变量
◆ 使用自旋锁的线程会反复检查锁变量是否可用
◆ 自旋锁不会让出CPU,是一种忙等待状态

特点

◆ **自旋锁避免了进程或线程上下文切换的开销【因为不让cpu,一直处于运行状态】
◆ 操作系统内部很多地方使用的是自旋锁
◆ 自旋锁不适合在单核CPU使用【自旋锁不会释放cpu,单核情况下别的进程就一直无法使用】**

实际操作

◆ pthread_spinlock_t
◆ pthread_ spinlock _lock
◆ pthread_ spinlock _unlock

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <vector>
  6. //定义自旋锁
  7. pthread_spinlock_t spin_lock;
  8. int num = 0;
  9. void *producer(void*){
  10. int times = 10000000;
  11. while(times --){
  12. pthread_spin_lock(&spin_lock);
  13. num += 1;
  14. pthread_spin_unlock(&spin_lock);
  15. }
  16. }
  17. void *comsumer(void*){
  18. int times = 10000000;
  19. while(times --){
  20. pthread_spin_lock(&spin_lock);
  21. num -= 1;
  22. pthread_spin_unlock(&spin_lock);
  23. }
  24. }
  25. int main(){
  26. printf("Start in main function.\n");
  27. pthread_spin_init(&spin_lock, 0);
  28. pthread_t thread1, thread2;
  29. pthread_create(&thread1, NULL, &producer, NULL);
  30. pthread_create(&thread2, NULL, &comsumer, NULL);
  31. pthread_join(thread1, NULL);
  32. pthread_join(thread2, NULL);
  33. printf("Print in main function: num = %d\n", num);
  34. return 0;
  35. }

读写锁

引入

Image 39.png

概述

◆ 读写锁是一种特殊的自旋锁
◆ 允许多个读者同时访问资源以提高读性能,多读少写时性能高很多
◆ 对于写操作则是互斥的
Image 41.png

实际操作

◆ pthread_rwlock_t
◆ pthread_rwlock_rdlock(读锁)
◆ pthread_rwlock_wrlock(写锁)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <vector>
  6. int num = 0;
  7. //定义互斥量:与用读写锁做对比
  8. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  9. //定义读写锁
  10. pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
  11. //读操作,把num打印出来
  12. void *reader(void*){
  13. int times = 10000000;
  14. while(times --){
  15. //读锁
  16. pthread_rwlock_rdlock(&rwlock);
  17. //pthread_mutex_lock(&mutex);
  18. if (times % 1000 == 0){
  19. printf("print num in reader: num = %d\n", num);
  20. usleep(10);
  21. }
  22. //pthread_mutex_unlock(&mutex);
  23. //释放锁
  24. pthread_rwlock_unlock(&rwlock);
  25. }
  26. }
  27. //写操作,每次给num+1
  28. void *writer(void*){
  29. int times = 10000000;
  30. while(times --){
  31. //pthread_mutex_lock(&mutex);
  32. pthread_rwlock_wrlock(&rwlock);
  33. num += 1;
  34. pthread_rwlock_unlock(&rwlock);
  35. //pthread_mutex_unlock(&mutex);
  36. }
  37. }
  38. int main(){
  39. printf("Start in main function.\n");
  40. pthread_t thread1, thread2, thread3;
  41. pthread_create(&thread1, NULL, &reader, NULL);
  42. pthread_create(&thread2, NULL, &reader, NULL);
  43. pthread_create(&thread3, NULL, &writer, NULL);
  44. pthread_join(thread1, NULL);
  45. pthread_join(thread2, NULL);
  46. pthread_join(thread3, NULL);
  47. printf("Print in main function: num = %d\n", num);
  48. return 0;
  49. }

条件变量

概述

◆ 条件变量是一种相对复杂的线程同步方法
◆ **条件变量允许线程睡眠,直到满足某种条件
◆ **当满足条件时,可以向该线程信号,通知唤醒

以生产者消费者举例

◆ 缓冲区小于等于0时,不允许消费者消费,消费者必须等待
◆ 缓冲区满时,不允许生产者往缓冲区生产,生产者必须等待
◆ 当生产者生产一个产品时,唤醒可能等待的消费者
◆ 当消费者消费一个产品时,唤醒可能等待的生产者

实际操作

◆ 配合互斥量使用
◆ pthread_cond_t
◆ pthread_cond_wait(等待条件满足)
◆ pthread_cond_signal(等待被唤醒)

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <queue>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8. //缓冲区大小
  9. int MAX_BUF = 100;
  10. int num = 0;
  11. //定义条件变量
  12. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  13. //定义互斥量
  14. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  15. void* producer(void*){
  16. while(true){
  17. //加锁
  18. pthread_mutex_lock(&mutex);
  19. //判断生产的是否大于缓冲区
  20. while (num >= MAX_BUF){
  21. // 等待
  22. printf("缓冲区满了, 等待消费者消费...\n");
  23. pthread_cond_wait(&cond, &mutex);
  24. }
  25. num += 1;
  26. printf("生产一个产品,当前产品数量为:%d\n", num);
  27. sleep(1);
  28. //通知消费者
  29. pthread_cond_signal(&cond);
  30. printf("通知消费者...\n");
  31. //解锁
  32. pthread_mutex_unlock(&mutex);
  33. sleep(1);
  34. }
  35. }
  36. void* consumer(void*){
  37. while(true){
  38. //加锁
  39. pthread_mutex_lock(&mutex);
  40. //判断是否没有产品可以消费
  41. while (num <= 0){
  42. // 等待
  43. printf("缓冲区空了, 等待生产者生产...\n");
  44. pthread_cond_wait(&cond, &mutex);
  45. }
  46. num -= 1;
  47. printf("消费一个产品,当前产品数量为:%d\n", num);
  48. sleep(1);
  49. //通知生产者
  50. pthread_cond_signal(&cond);
  51. printf("通知生产者...\n");
  52. pthread_mutex_unlock(&mutex);
  53. }
  54. }
  55. int main(){
  56. pthread_t thread1, thread2;
  57. pthread_create(&thread1, NULL, &consumer, NULL);
  58. pthread_create(&thread2, NULL, &producer, NULL);
  59. pthread_join(thread1, NULL);
  60. pthread_join(thread2, NULL);
  61. return 0;
  62. }

总结

Image 42.png
Image 43.png
Image 44.png