一、互斥量——mutex
1、为什么需要互斥量?
1)大部分情况,线程使用的数据都是局部变量,变量的地址空间在线程栈空间内,这种情况变量归属单个线程,其他线程无法获得变量。
2)但有的时候,很多变量都需要在线程间共享,这样的变量称为共享变量,可以通过数据的共享,完成线程间的交互。
3)多个线程并发的操作共享变量,会带来一些问题!
【例】存在共享变量问题的售票系统代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
int ticket = 10;
void *route(void *arg)
{
char *id = (char*)arg;
while(1){
if(ticket > 0){
usleep(100000);
printf("%s sells ticket:%d\n", id, ticket);
ticket--;
}else{
break;
}
}
}
int main(void)
{
pthread_t tid1, tid2, tid3, tid4;
pthread_create(&tid1, NULL, route, "thread 1");
pthread_create(&tid2, NULL, route, "thread 2");
pthread_create(&tid3, NULL, route, "thread 3");
pthread_create(&tid4, NULL, route, "thread 4");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_join(tid4, NULL);
}
运行结果:
为什么可能无法获得正确的结果?
(1)if语句判断条件为真以后,代码可以并发的切换到其他线程;
(2)Usleep这个模拟漫长业务的过程,在这个漫长的业务过程中,可能有很多个线程会进入该代码段;
(3)ticket操作本身就不是一个原子操作。
【例】取出ticket的部分汇编代码
可见:操作并不是原子操作,而是对应三条汇编指令
1)load:将共享变量ticket从内存加载到寄存器中;
2)Update:更新寄存器里面的值,执行-1操作;
3)Store:将新值从寄存器写到共享变量ticket的内存地址。
要解决以上问题,需要做到三点:
1)代码必须要有互斥行为:当代码进入临界区执行时,不允许其他线程进入该临界区。
2)如果多个线程同时要求执行临界区代码,并且临界区没有线程执行,那么只能允许一个线程进入临界区。
3)如果线程不在临界区中执行,那么该线程不能阻止其他线程进入临界区。
要做到这三点,本质上就是需要一把锁。Linux上提供的这把锁叫做互斥量。如下图所示:
2、互斥量的接口
(1)初始化互斥量
初始化互斥量有两种方法:
1)方法一:静态分配:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
2)方法二:动态分配:
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
参数:
mutex:要初始化的互斥量;
attr:NULL。
(2)销毁互斥量——pthread_mutex_destroy
int pthread_mutex_destroy(pthread_mutex_t *mutex);
1)销毁互斥量需要注意:
A、使用PTHREAD_MUTEX_INITIALIZER初始化的互斥量不需要销毁;
B、不要销毁一个已经加锁的互斥量;
C、已经销毁的互斥量,要确保后面不会有线程在尝试加锁。
(3)互斥量加锁和解锁
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
返回值:成功返回0,失败返回错误号
调用pthread_lock时,可能会遇到以下情况:
1)互斥量处于未锁状态,该函数会将互斥量锁定,同时返回成功。
2)发起函数调用时,其他线程已经锁定互斥量,或者存在其他线程同时申请互斥量,但没有竞争带互斥量,那么pthread_lock调用会陷入阻塞,等待互斥量解锁。
【例】改进上面的售票系统
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<sched.h>
int ticket = 10;
pthread_mutex_t mutex;
void *route(void *arg)
{
char *id = (char*)arg;
while(1){
pthread_mutex_lock(&mutex);
if(ticket > 0){
usleep(1000);
printf("%s sells ticket:%d\n", id, ticket);
ticket--;
pthread_mutex_unlock(&mutex);
sched_yield();//需注意!
}else{
pthread_mutex_unlock(&mutex);
break;
}
}
}
int main(void)
{
pthread_t tid1, tid2, tid3, tid4;
pthread_mutex_init(&mutex, NULL);
pthread_create(&tid1, NULL, route, "thread 1");
pthread_create(&tid2, NULL, route, "thread 2");
pthread_create(&tid3, NULL, route, "thread 3");
pthread_create(&tid4, NULL, route, "thread 4");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_join(tid4, NULL);
pthread_mutex_destroy(&mutex);
}
释:sched_yield()这个函数可以让等于或高于当前线程的线程先运行。如果没有符合条件的线程,那么这个函数将会立刻返回然后继续执行当前线程的程序。 在成功完成之后返回零,否则返回-1。
运行结果: