Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
Will this example code for g_cond_wait() lead to undefined behaviour?

This is an example from the Glib docs for g_cond_wait():

gpointer current_data = NULL;
GMutex data_mutex;
GCond data_cond;

void push_data (gpointer data)
{
  g_mutex_lock (&data_mutex); // (3)
  current_data = data;
  g_cond_signal (&data_cond);
  g_mutex_unlock (&data_mutex); // (4)
}

gpointer pop_data (void)
{
  gpointer data;

  g_mutex_lock (&data_mutex); // (1)
  while (!current_data)
    g_cond_wait (&data_cond, &data_mutex); // (2)
  data = current_data;
  current_data = NULL;
  g_mutex_unlock (&data_mutex); // (5)

  return data;
}

Let's now go through this:

  • First thread calls pop_data(), data_mutex is locked (1)
  • g_cond_wait() is called, data_mutex is unlocked (2), first thread is waiting
  • Second thread calls push_data(), data_mutex is locked (3)
  • Second thread signals the first one that the wait condition is satisfied, unlocks data_mutex (4)
  • First thread wakes up, exits from g_cond_wait() and unlocks data_mutex again (5)

The docs say that unlocking a non-locked mutex is undefined. Does this mean the example contains a bug? Or will g_cond_wait() lock the mutex before exiting?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The docs say that unlocking a non-locked mutex is undefined. Does this mean the example contains a bug? Or will g_cond_wait() lock the mutex before exiting?

There's no bug here. There's no unlocking of a mutex which wasn't locked in the code. g_cond_wait() will lock the mutex when it returns.

Thread1 calls g_cond_wait() with mutex locked and g_cond_wait() atomically unlocks the mutex and waits on the condition. After this, thread2 locks the mutex and performs the operation, then signals to thread1 which is waiting in g_cond_wait(). But thread1 can't continue as the mutex is still not available (thread2 is yet to unlock it). So after the call g_mutex_unlock() in thread2 unlocks it, g_cond_wait() in thread1 locks the mutex and the call returns.

over 4 years ago · Santiago Trujillo Report

0

Yes, the mutex is locked again after the wait. From the glib docs for g_cond_wait():

When this function returns, mutex is locked again and owned by the calling thread.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!