怎么提高网站排名,专业做网站咨询,网站建设工作分解,微信支付 网站备案一、互斥量mutex
Linux提供一把互斥锁mutex(也称之为互斥量)每个线程在对资源操作前都尝试先加锁#xff0c;成功加锁才能操作#xff0c;操作结束后解锁。资源还是共享的#xff0c;线程间也还是竞争的#xff0c;但通过锁将资源的访问变为互斥操作#xff0c;而后与时间…一、互斥量mutex
Linux提供一把互斥锁mutex(也称之为互斥量)每个线程在对资源操作前都尝试先加锁成功加锁才能操作操作结束后解锁。资源还是共享的线程间也还是竞争的但通过锁将资源的访问变为互斥操作而后与时间有关的错误也不会在产生了。
如图所示 但是应该注意同一个时刻只能有一个线程持有该锁。
当A线程对某个全局变量加锁访问B在访问前尝试加锁拿不到锁B阻塞。C线程不去加锁而直接访问该全局变量依然能够访问但会出现数据混乱。
所以互斥锁实质上是是操作系统提供的一把“建议锁”(又称“协同所”)建议程序中有多线程访问共享资源的时候使用该机制但是并没有强制限定。 二、主要应用函数
分析
pthread_mutex_t 类型其本质是一个结构体为简化理解应用时可忽略其实现细节简单当成整数看待。pthread_mutex_t mutex变量mutex只有两种取值0、1
1. 函数原型
功能初始化一个互斥锁(互斥量) 初值可看做1
#includepthread.h
pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 返回值若成功返回0否则返回错误编号
参数1传出参数调用时应传mutex
restrict关键字只用于限制指针告诉编译器所有修改该指针指向内存中内容的操作只能通过本指针完成。不能通过除本指针以外的其他变量或指针修改。
参数2互斥属性。是一个传入参数通常传NULL选用默认属性(线程间共享).
静态初始化如果互斥锁mutex是静态分配的(定义在全局或加了static关键字修饰可以直接使用宏进行初始化。pthread_mutex_t mutex PTHREAD_MUTEX_INITIALIZER;动态初始化局部变量应采用动态初始化。pthread_mutex_init(mutex, NULL);2. 函数原型 功能销毁一个互斥锁
#includepthread.h
pthread_mutex_destroy(pthread_mutex_t *mutex); 返回值若成功返回0否则返回错误编号 3. 函数原型 功能加锁。可理解为将mutex--或-1
#includepthread.h
pthread_mutex_lock(pthread_mutex_t *mutex);返回值若成功返回0否则返回错误编号
分析
没有被上锁当前线程会将这把锁锁上被锁上了当前线程阻塞锁被打开之后线程解除阻塞。4. 函数原型 功能解锁。可理解为将mtex或1
#includepthread.h
pthread_mutex_unlock(pthread_mutex_t *mutex);返回值若成功返回0否则返回错误编号
注意
同时将阻塞在该锁上的所有线程全部唤醒5. 函数原型
功能尝试加锁, 失败返回, 不阻塞
#includepthread.h
pthread_mutex_trylock(pthread_mutex_t *mutex);返回值若成功返回0否则返回错误编号
分析
没有锁上当前线程会给这把锁加锁如果锁上了不会阻塞返回三、程序清单(一
1. 测试代码
#include stdio.h
#include string.h
#include pthread.h
#include stdlib.h
#include unistd.hvoid *tfn(void *arg)
{srand(time(NULL));while(1) {printf(hello );sleep(rand() % 3); //模拟长时间操作共享资源导致cpu易主产生与时间有关的错误printf(word\n);sleep(rand() % 3);}return NULL;
}int main()
{pthread_t tid;srand(time(NULL));pthread_create(tid, NULL, tfn, NULL);while(1) {printf(HELLO );sleep(rand() % 3);printf(WORLD\n);sleep(rand() % 3);}return 0;
}
运行结果 2. 测试代码
#includepthread.h
#includestdlib.h
#includeunistd.hpthread_mutex_t mutex;void *tfn(void *arg)
{srand(time(NULL));while(1) {pthread_mutex_lock(mutex);printf(hello );sleep(rand() % 3); //模拟长时间共享资源导致cpu易主。产生于时间有关的错误 printf(word\n);pthread_mutex_unlock(mutex);sleep(rand() % 3);}return NULL;
}int main()
{pthread_t tid;srand(time(NULL));pthread_mutex_init(mutex, NULL);pthread_create(tid, NULL, tfn, NULL); //mutex 1while(1) {pthread_mutex_lock(mutex);printf(HELLO );sleep(rand() % 3);printf(WORLD\n);pthread_mutex_unlock(mutex);sleep(rand() % 3);}pthread_mutex_destroy(mutex);return 0;
}
输出结果 3. 测试代码
#includepthread.h
#includestdlib.h
#includeunistd.hpthread_mutex_t mutex;void *tfn(void *arg)
{srand(time(NULL));while(1) {pthread_mutex_lock(mutex);printf(hello );sleep(rand() % 3); //模拟长时间共享资源导致cpu易 主。产生于时间有关的错误 printf(word\n);sleep(rand() % 3);pthread_mutex_unlock(mutex);}return NULL;
}int main()
{pthread_t tid;srand(time(NULL));pthread_mutex_init(mutex, NULL);pthread_create(tid, NULL, tfn, NULL); //mutex 1while(1) {pthread_mutex_lock(mutex);printf(HELLO );sleep(rand() % 3);printf(WORLD\n); sleep(rand() % 3);pthread_mutex_unlock(mutex);}pthread_mutex_destroy(mutex);return 0;
}输出结果 四、程序清单(二
四、加锁与解锁
lock与unlock
lock尝试加锁如果加锁不成功线程阻塞阻塞到持有该互斥量的其他线程解锁为止。unlock主动解锁同时将阻塞到该锁上所有线程全部唤醒至于哪个线程先被唤醒取决于优先级调度。默认先阻塞、先唤醒。
例如T1 T2 T3 T4 使用一把mutex锁T1加锁成功其他线程均阻塞直至T1解锁T1解锁后T2 T3 T4均被唤醒并自动再次尝试加锁。
可假想mutex锁init成功初值为1。lock功能是将mutex--。unlock将mutex。 lock与trylock
lock加锁失败会阻塞等待锁释放。trylock加锁失败直接返回错误号如EBUSY不阻塞。注意在访问共享资源前加锁访问结束后立即解锁。锁的“粒度”越小越好。