作者:手机用户2502880237 | 来源:互联网 | 2023-05-17 20:23
1threadpthread_mutex_lock(&mutex);pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,
//1 thread
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex);
//2 thread
pthread_mutex_lock(&mutex);
pthread_cond_signal(&mutex);
pthread_mutex_unlock(&mutex);
//3 init
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &attr);
先执行1 thread,然后执行2 thread,会发现在锁定1 thread后,进行pthread_cond_wait操作后,会导致死锁
猜想,应该是pthread_cond_wait只会解锁一层mutex,最后还有一层锁定状态,导致死锁状态
另还有文档说明不要使用pthread_cond_wait在递归锁(重入锁)中