一、线程同步方法:
1、互斥量 互斥锁(mutex)
- 互斥量保证先后执行;
- 互斥量保证了关键指令的原子性; (一系列操作不可被中断的特性;要么全部执行,要么全部没执行)
- 互斥量是线程同步的最简单的方法;
- 互斥量、互斥锁,他是处于两态之一的变量:解锁、加锁;
- 两个状态可以保证资源访问的串行性;
2、自旋锁(spin_lock)
- 原理和互斥锁一致,使用前加锁;
- 使用自旋锁的线程会反复检查锁变量是否可用;
- 自旋锁不会让出CPU ,是一种忙等待的状态;相当于死循环等锁开;
优缺点:
- 自旋锁避免了进程、线程上下文切换的开销;
- 操作系统内部很多使用的是自旋锁;
- 自旋锁不适合单核CPU;因为占用CPU;
3、读写锁
(rwlock_t、rwlock_rdlock、rwlock_wrlock)
- 针对临界资源多读少写;
- 读取的时候不会改变临界资源的值;
- 读写锁是一种特殊自旋锁;
- 允许多个读者同时访问资源,以提高读性能;仅对读操作互斥;
- 读的时候读锁;写的时候写锁;
优缺点:
- 自旋锁避免了进程、线程上下文切换的开销;
- 操作系统内部很多使用的是自旋锁;
- 自旋锁不适合单核CPU;因为占用CPU;
- 比互斥锁快的多,针对的环境不同;
4、条件变量
cond_t ;cond_wait;cond_signal;
- 条件变量允许线程睡眠,直到满足某种条件;
- 当满足条件时,可以向该线程发出信号,通知唤醒;
- 需要配合互斥量使用;
这其实是因为前面的锁没有提及的问题:
- 缓冲器小于0,不允许消费者消费,消费者必须等待;
- 缓冲区满时,不允许生产者往缓冲区生产,生产者必须等待;
当生产者生产了一个产品的时候,唤醒可能等待的消费者;
当消费者消费了一个产品的时候,唤醒可能等待的生产者;
- wait 等待条件满足
- signal 发送唤醒
二、线程同步方法比较
临界区linux 没有,windows有;更轻量级;在用户态实现;
三、线程同步实例
1、互斥锁
windows 下的mutex如下:
linux 下的mutex 一致,在下面条件变量使用的时候也有用到;
#include <mutex>
std::mutex mymutex; //互斥锁
int nums = 0;
void preducer()
{
cout << "preducer() begin" << endl;
int times = 1000000;
while (times--)
{
mymutex.lock();
nums += 1;
mymutex.unlock();
}
}
void comsumer()
{
cout << "comsumer() begin" << endl;
int times = 1000000;
while (times--)
{
mymutex.lock();
nums -= 1;
mymutex.unlock();
}
}
void main()
{
cout << " start " << endl;
thread thread1(preducer), thread2(comsumer);
thread1.join();
thread2.join();
cout << nums << endl;
cout << " end " << endl;
}
效果
start
preducer() begin
comsumer() begin
0
end
2、自旋锁
linux 下的spin lock
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <iostream>
using namespace std;
pthread_spinlock_t spin_lock;
int num = 0;
void *producer(void*)
{
int times = 1000000;
while(times--)
{
pthread_spin_lock(&spin_lock);
num+=1;
pthread_spin_unlock(&spin_lock);
}
}
void *comsumer(void*)
{
int times = 1000000;
while(times--)
{
pthread_spin_lock(&spin_lock);
num-=1;
pthread_spin_unlock(&spin_lock);
}
}
int main()
{
std::cout<<"start !"<<endl;
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);
cout<<"num = "<<num<<endl;
return 0;
}
g++ main.cpp -o main1 -g -lpthread
3、读写锁
linux 下的rwlock
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <iostream>
using namespace std;
int num = 0;
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
void *writer(void*)
{
int times = 10000000;
while(times--)
{
pthread_rwlock_wrlock(&rwlock);
num+=1;
pthread_rwlock_unlock(&rwlock);
}
}
void *reader(void*)
{
int times = 10000;
while(times--)
{
pthread_rwlock_rdlock(&rwlock);
cout<<" num = "<<num<<endl;
usleep(10);
pthread_rwlock_unlock(&rwlock);
}
}
int main()
{
std::cout<<"start !"<<endl;
pthread_t thread1,thread2,thread3;
pthread_create(&thread1,NULL,&writer,NULL);
pthread_create(&thread2,NULL,&reader,NULL);
pthread_create(&thread3,NULL,&reader,NULL);
pthread_join(thread3,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
cout<<"num = "<<num<<endl;
return 0;
}
g++ main2.cpp -o main2 -g -lpthread
4、条件变量
linux 下的cond 和mutex
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <iostream>
using namespace std;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int num = 0;
int MAX_BUF = 100;
void *producer(void*)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (num > MAX_BUF)
{
pthread_cond_wait(&cond,&mutex);
cout << "缓冲区满了,等待消费" << endl;
}
num += 1;
cout << "生产ing num = "<<num << endl;
sleep(1);
pthread_cond_signal(&cond);
cout << "通知消费者 " << endl;
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void *comsumer(void*)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (num <=0)
{
pthread_cond_wait(&cond, &mutex);
cout << "缓冲区空了,等待生产" << endl;
}
num -= 1;
cout << "消费ing num = " << num << endl;
sleep(1);
pthread_cond_signal(&cond);
cout << "通知消费者 " << endl;
pthread_mutex_unlock(&mutex);
}
}
int main()
{
std::cout << "start !" << endl;
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, &producer, NULL);
pthread_create(&thread2, NULL, &comsumer, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
cout << "num = " << num << endl;
return 0;
}
g++ main2.cpp -o main2 -g -lpthread