方法概览
互斥量
原子性
◆ 原子性是指一系列操作不可被中断的特性
◆ 这一系列操作要么全部执行完成,要么全部没有执行
◆ 不存在部分执行部分未执行的情况
概述
互斥量可以保证两个线程指令的先后执行**,保证关键操作的原子性,通俗的来说就是一串指令执行过程中,不让别的指令插入执行,这一串指令执行完,下一串指令才能执行。
两个进程访问临界资源,保证对临界资源的访问是线性的
◆ 互斥量是最简单的线程同步的方法
◆ 互斥量(互斥锁),处于两态之一的变量:解锁和加锁
◆** 一个资源加锁了,就表示这个资源被使用着,另外一个进程想使用,只能等待正在使用的进程解锁
◆ 两个状态可以保证资源访问的串行
实际操作
◆ 操作系统直接提供了互斥量的API
◆ 开发者可以直接使用API完成资源的加锁、解锁操作
◆ pthread_mutex_t
◆ pthread_mutex_lock
◆ pthread_mutex_unlock
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <vector>//定义互斥量pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;// 临界资源int num = 0;// 生产者void *producer(void*){int times = 100000000;while(times --){//加锁pthread_mutex_lock(&mutex);num += 1;//解锁pthread_mutex_unlock(&mutex);}}// 消费者void *comsumer(void*){int times = 100000000;while(times --){pthread_mutex_lock(&mutex);num -= 1;pthread_mutex_unlock(&mutex);}}int main(){printf("Start in main function.");pthread_t thread1, thread2;pthread_create(&thread1, NULL, &producer, NULL);pthread_create(&thread2, NULL, &comsumer, NULL);pthread_join(thread1, NULL);pthread_join(thread2, NULL);printf("Print in main function: num = %d\n", num);return 0;}
自旋锁
概述
自旋锁的原理和互斥锁原理是相似的,也是对临界资源加锁解锁,不同的是使用自旋锁,会一直拿着cpu,一直处于工作状态不停的问锁可不可以用
◆ 自旋锁也是一种多线程同步的变量
◆ 使用自旋锁的线程会反复检查锁变量是否可用
◆ 自旋锁不会让出CPU,是一种忙等待状态
特点
◆ **自旋锁避免了进程或线程上下文切换的开销【因为不让cpu,一直处于运行状态】
◆ 操作系统内部很多地方使用的是自旋锁
◆ 自旋锁不适合在单核CPU使用【自旋锁不会释放cpu,单核情况下别的进程就一直无法使用】**
实际操作
◆ pthread_spinlock_t
◆ pthread_ spinlock _lock
◆ pthread_ spinlock _unlock
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <vector>//定义自旋锁pthread_spinlock_t spin_lock;int num = 0;void *producer(void*){int times = 10000000;while(times --){pthread_spin_lock(&spin_lock);num += 1;pthread_spin_unlock(&spin_lock);}}void *comsumer(void*){int times = 10000000;while(times --){pthread_spin_lock(&spin_lock);num -= 1;pthread_spin_unlock(&spin_lock);}}int main(){printf("Start in main function.\n");pthread_spin_init(&spin_lock, 0);pthread_t thread1, thread2;pthread_create(&thread1, NULL, &producer, NULL);pthread_create(&thread2, NULL, &comsumer, NULL);pthread_join(thread1, NULL);pthread_join(thread2, NULL);printf("Print in main function: num = %d\n", num);return 0;}
读写锁
引入
概述
◆ 读写锁是一种特殊的自旋锁
◆ 允许多个读者同时访问资源以提高读性能,多读少写时性能高很多
◆ 对于写操作则是互斥的
实际操作
◆ pthread_rwlock_t
◆ pthread_rwlock_rdlock(读锁)
◆ pthread_rwlock_wrlock(写锁)
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <vector>int num = 0;//定义互斥量:与用读写锁做对比pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//定义读写锁pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;//读操作,把num打印出来void *reader(void*){int times = 10000000;while(times --){//读锁pthread_rwlock_rdlock(&rwlock);//pthread_mutex_lock(&mutex);if (times % 1000 == 0){printf("print num in reader: num = %d\n", num);usleep(10);}//pthread_mutex_unlock(&mutex);//释放锁pthread_rwlock_unlock(&rwlock);}}//写操作,每次给num+1void *writer(void*){int times = 10000000;while(times --){//pthread_mutex_lock(&mutex);pthread_rwlock_wrlock(&rwlock);num += 1;pthread_rwlock_unlock(&rwlock);//pthread_mutex_unlock(&mutex);}}int main(){printf("Start in main function.\n");pthread_t thread1, thread2, thread3;pthread_create(&thread1, NULL, &reader, NULL);pthread_create(&thread2, NULL, &reader, NULL);pthread_create(&thread3, NULL, &writer, NULL);pthread_join(thread1, NULL);pthread_join(thread2, NULL);pthread_join(thread3, NULL);printf("Print in main function: num = %d\n", num);return 0;}
条件变量
概述
◆ 条件变量是一种相对复杂的线程同步方法
◆ **条件变量允许线程睡眠,直到满足某种条件
◆ **当满足条件时,可以向该线程信号,通知唤醒
以生产者消费者举例
◆ 缓冲区小于等于0时,不允许消费者消费,消费者必须等待
◆ 缓冲区满时,不允许生产者往缓冲区生产,生产者必须等待
◆ 当生产者生产一个产品时,唤醒可能等待的消费者
◆ 当消费者消费一个产品时,唤醒可能等待的生产者
实际操作
◆ 配合互斥量使用
◆ pthread_cond_t
◆ pthread_cond_wait(等待条件满足)
◆ pthread_cond_signal(等待被唤醒)
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <vector>#include <queue>#include <unistd.h>#include <pthread.h>//缓冲区大小int MAX_BUF = 100;int num = 0;//定义条件变量pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//定义互斥量pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;void* producer(void*){while(true){//加锁pthread_mutex_lock(&mutex);//判断生产的是否大于缓冲区while (num >= MAX_BUF){// 等待printf("缓冲区满了, 等待消费者消费...\n");pthread_cond_wait(&cond, &mutex);}num += 1;printf("生产一个产品,当前产品数量为:%d\n", num);sleep(1);//通知消费者pthread_cond_signal(&cond);printf("通知消费者...\n");//解锁pthread_mutex_unlock(&mutex);sleep(1);}}void* consumer(void*){while(true){//加锁pthread_mutex_lock(&mutex);//判断是否没有产品可以消费while (num <= 0){// 等待printf("缓冲区空了, 等待生产者生产...\n");pthread_cond_wait(&cond, &mutex);}num -= 1;printf("消费一个产品,当前产品数量为:%d\n", num);sleep(1);//通知生产者pthread_cond_signal(&cond);printf("通知生产者...\n");pthread_mutex_unlock(&mutex);}}int main(){pthread_t thread1, thread2;pthread_create(&thread1, NULL, &consumer, NULL);pthread_create(&thread2, NULL, &producer, NULL);pthread_join(thread1, NULL);pthread_join(thread2, NULL);return 0;}
总结



