说递归互斥量前,说下互斥量都有哪些,apue第三版上说有下面4种:
-
pthread_mutex_normal:标准类型,不做任何特殊的错误检查或者死锁检测。
在同一个线程里去锁一个还没有解锁的互斥量时,发生死锁。
-
pthread_mutex_recursive:递归类型。
此互斥量类型允许同一线程在互斥量解锁前对该互斥量进行多次加锁。递归互斥量维护锁的计数,在解锁次数和加锁次数不相同的情况下,不会释放锁,别的线程就无法加锁此互斥量。
-
pthread_mutex_errorcheck:提供错误检测。如果在同一个线程里去锁一个还没有解锁的互斥量,会报告错误。但在centos7(3.10.0-957.el7.x86_64),gcc (gcc) 4.8.5 20150623 (red hat 4.8.5-39)上测试过,发现:在同一个线程里去锁一个还没有解锁的互斥量,没有报告错误。
-
pthread_mutex_default
gcc (gcc) 4.8.5 20150623 (red hat 4.8.5-39)环境里pthread.h里,互斥量类型的定义如下:
/* mutex types. */ enum { pthread_mutex_timed_np, pthread_mutex_recursive_np, pthread_mutex_errorcheck_np, pthread_mutex_adaptive_np #if defined __use_unix98 || defined __use_xopen2k8 , pthread_mutex_normal = pthread_mutex_timed_np, pthread_mutex_recursive = pthread_mutex_recursive_np, pthread_mutex_errorcheck = pthread_mutex_errorcheck_np, pthread_mutex_default = pthread_mutex_normal #endif #ifdef __use_gnu /* for compatibility. */ , pthread_mutex_fast_np = pthread_mutex_timed_np #endif };
下面的例子验证递归互斥量.
例子很简单,在main函数里创建2个线程,在线程1的函数fn1,加锁互斥量2次,但是只解锁一次。线程fn2就无法给互斥量加锁,导致一直阻塞在①处。
为了能够让线程fn1能够先给互斥量加锁,在fn2里调用了sleep函数,让fn2先睡眠1秒,所以fn1就能够先给互斥量加锁了。
去掉②处的注释,fn2就能锁定mutex了,程序就不会出现死锁状态了。
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <signal.h> pthread_mutex_t mt; int i = 0; void* fn1(void* agr) { int err; pthread_mutex_lock(&mt); if((err = pthread_mutex_lock(&mt)) < 0) { printf("%sn", strerror(err)); exit(1); } ++i; printf("%dn", i); //pthread_mutex_unlock(&mt);//-------② pthread_mutex_unlock(&mt); } void* fn2(void* arg) { sleep(1);//目的是让线程fn1先执行。 pthread_mutex_lock(&mt);//-----------① ++i; printf("second %dn", i); pthread_mutex_unlock(&mt); } int main() { pthread_t tid1, tid2; pthread_mutexattr_t mat; pthread_mutexattr_init(&mat); //设置锁的类型为递归锁 pthread_mutexattr_settype(&mat, pthread_mutex_recursive); pthread_mutex_init(&mt, &mat); pthread_create(&tid1, null, fn1, null); pthread_create(&tid2, null, fn2, null); pthread_join(tid1, null); pthread_join(tid2, null); pthread_mutex_destroy(&mt); }
c/c++ 学习互助qq群:877684253 本人微信:xiaoshitou5854
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/600019.html