简体   繁体   English

为什么在pthread中使用互斥时会出现死锁?

[英]Why there is deadlock while using mutex in pthread?

Pthread program given below demonstrate the mutex example in pthread. 下面给出的Pthread程序演示了pthread中的互斥示例。 But while running this code deadlock is occurring for most of the time giving right result for some runs. 但是在运行此代码时,大多数情况下会发生死锁,从而为某些运行提供正确的结果。

#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
volatile int sv=10;
volatile int x,y,temp=10;
pthread_mutex_t mut;
pthread_cond_t con;
void *ChildThread(void *arg)
{
pthread_mutex_lock (&mut);
pthread_cond_wait(&con, &mut);  
x=sv;
x++;
sv=x;
printf("The child sv is %d\n",sv);
pthread_mutex_unlock (&mut);
pthread_exit(NULL);
}

void *ChildThread2(void *arg)
{
pthread_mutex_lock (&mut);
y=sv;
y--;
sv=y;
printf("The child2 sv is %d\n",sv); 
pthread_cond_signal(&con);
pthread_mutex_unlock (&mut);
pthread_exit(NULL);
}

int main(void)
{
pthread_t child1,child2,child3;
pthread_mutex_init(&mut, NULL);
pthread_create(&child2,NULL,ChildThread2,NULL);
pthread_create(&child1,NULL,ChildThread,NULL);
pthread_cond_destroy(&con);
pthread_mutex_destroy(&mut);
pthread_join(child1,NULL);
pthread_join(child2,NULL);
return 0;
}

Your deadlock here is likely caused by misuse of pthread_cond_wait() . 这里的死锁可能是由于误用pthread_cond_wait()造成的。 You typically want to call pthread_cond_wait() in a loop, checking a condition, eg. 您通常希望在循环中调用pthread_cond_wait() ,检查条件,例如。

while (!cond)
    pthread_cond_wait(...);

As pthread_cond_signal() will only wake a thread sleeping on pthread_cond_wait() at that time, if the signalling thread runs first, the waiter will never wake up. 由于pthread_cond_signal()只会唤醒当时在pthread_cond_wait()上休眠的线程,如果信令线程先运行,服务员将永远不会唤醒。

Other problems here are that you're destroying your condition variable and mutex before the threads are done, which generally leads to unexpected behavior. 这里的其他问题是你在线程完成之前破坏你的条件变量和互斥量,这通常会导致意外的行为。 You should pthread_join() the threads before taking away their resources. pthread_join()资源之前,你应该pthread_join()线程。 You also never initialize the condition variable. 您也永远不会初始化条件变量。

One issue is that you never initialize the condition variable. 一个问题是你永远不会初始化条件变量。 Another is that you destroy the mutex (and try to destroy the uninitialized condition variable) while the threads that use them could still be running. 另一个是你破坏互斥锁(并尝试销毁未初始化的条件变量),而使用它们的线程仍然可以运行。

There is no need to initialize static mutexes or conditions dynamically with the init functions, nor to destroy them at the end. 不需要使用init函数动态初始化静态互斥锁或条件,也不需要在最后销毁它们。 Use PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER at the definition of your variables mut and cond : 在变量mutcond的定义中使用PTHREAD_MUTEX_INITIALIZERPTHREAD_COND_INITIALIZER

pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con = PTHREAD_COND_INITIALIZER;

And then erase all calls to _init and _destroy functions. 然后擦除对_init_destroy函数的所有调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM