c - pthread_mutex_lock 和 pthread_cond_wait/signal 导致死锁

标签 c multithreading mutex

我在我的程序中使用了 2 个线程,一个用于打印偶数,另一个用于顺序打印奇数。当我运行下面的代码时,程序在打印 0 和 1 后阻塞。看起来像是死锁。
但是,如果我将 rc=pthread_mutex_lock(&mutex) 移动到 PrintEvenNos() 和 PrintOddNos() 中的 while 语句之上,则输出是连续且完整的(根据需要)。 有人可以解释为什么它在第一种情况下失败以及导致死锁的原因吗?

#include<stdio.h>
#include<pthread.h>

pthread_t tid[2];
unsigned int shared_data = 0;
pthread_mutex_t mutex;
pthread_cond_t even,odd;
unsigned int rc;

void* PrintEvenNos(void*);
void* PrintOddNos(void*);

void main(void)
{
    pthread_create(&tid[0],0,&PrintEvenNos,0);

    pthread_create(&tid[1],0,&PrintOddNos,0);
    sleep(3);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
}

void* PrintEvenNos(void *ptr)
{
    //rc = pthread_mutex_lock(&mutex);     /*works when I uncomment here and comment the next mutex_lock */
      while (shared_data <= 5)
     {rc = pthread_mutex_lock(&mutex);
      if(shared_data%2 == 0)
       { printf("t1.....................................Even:%d\n",shared_data);
         shared_data++;
         pthread_cond_signal(&odd);
         rc=pthread_mutex_unlock(&mutex);
   }
     else
     {
           pthread_cond_wait(&even, &mutex);
     }
 }
  rc=pthread_mutex_unlock(&mutex);
}

void* PrintOddNos(void* ptr1)
{
// rc = pthread_mutex_lock(&mutex);   /*works when I uncomment here and comment the next mutex_lock */
   while (shared_data <= 5)
    {
     rc = pthread_mutex_lock(&mutex);
     if(shared_data%2 != 0)
        {
        printf("t2.....................................odd:%d\n",shared_data);
            shared_data++;
            pthread_cond_signal(&even);
            rc=pthread_mutex_unlock(&mutex);
        }
        else
            {
               pthread_cond_wait(&odd, &mutex);
        }
    }
 rc=pthread_mutex_unlock(&mutex);
}

最佳答案

您的程序具有未定义的行为,因为您正在等待的线程在从 pthread_cond_wait 返回时重新获取锁,然后在它已经拥有的互斥量上再次调用 pthread_mutex_lock

互斥体应该按照您在评论中指出的那样使用。这正是 pthread_cond_wait 的用途:它在进入时释放锁并在返回时重新获取它。

另外,把if分支的pthread_mutex_unlock去掉,是错误的。通常,您应该只有一对标记关键部分的 lock/unlock 调用。

关于c - pthread_mutex_lock 和 pthread_cond_wait/signal 导致死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42628107/

相关文章:

c++ - 为什么编译器说 ‘pthread_getthreadid_np’ 没有在此范围内声明?

c - 如何提供一系列交错线程来表明代码中断并且没有提供完美的同步?

c - 异步信号导致套接字 I/O 问题

c++ - cygwin pthread_mutex_timedlock 代理

linux - 在 Linux 中编写我的第一个漏洞利用程序

python - 无法终止 PyQT QThread

multithreading - 引用队列中的前数据包时程序终止

c++ - 左移带有可变误差

c++ - 如何访问和分配结构上的 int 指针数据类型的值?

c - OpenGL SOIL 连接到 Visual Studio