C: why calling pthread_cond_signal hangs?

  Kiến thức lập trình

I have the following code snippet. Note, I have multiple producers and only one consumer.

pthread_mutex_t mutex;
pthread_cond_t cv;


// producer thread

if (pthread_mutex_lock(&mutex) != 0)
{
   log(LOG_ERR, "pthread_mutex_lock: %s (%d)", strerror(errno), errno);
}

// do something

if (pthread_mutex_unlock(&mutex) != 0)
{
   log(LOG_ERR, "pthread_mutex_unlock: %s (%d)", strerror(errno), errno);
}

if (pthread_cond_signal(&cv) != 0)
{
   log(LOG_ERR, "pthread_cond_signal: %s (%d)", strerror(errno), errno);
}


// consumer thread

if (pthread_mutex_lock(&mutex) != 0)
{
   log(LOG_ERR, "pthread_mutex_lock: %s (%d)", strerror(errno), errno);
}

while (1)
{
   if (variable)
   {
      break;
   }

   if (pthread_cond_wait(&cv, &mutex) != 0)
   {
      log(LOG_ERR, "pthread_cond_wait: %s (%d)", strerror(errno), errno);
   }
}

// do something

if (pthread_mutex_unlock(&mutex) != 0)
{
   log(LOG_ERR, "pthread_mutex_unlock: %s (%d)", strerror(errno), errno);
}

At first, it runs pretty well, but after a while all producers hang at pthread_cond_signal and consumer hangs at pthread_cond_wait, which I verify using gdb attach. The log does not report any error.

Below is the output from strace which has no output update.

$ strace -k -p 1234567
strace: Process 1234567 attached
futex(0x635ff0, FUTEX_WAIT_PRIVATE, 2, NULL

LEAVE A COMMENT