From: Colin Walters Date: Thu, 23 May 2013 00:07:13 +0000 (+0100) Subject: glib/tests/cond: Fix race condition X-Git-Tag: 2.37.1~28 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ff8f37ac05490c67fc3be5e87ff5756f1a275da3;p=platform%2Fupstream%2Fglib.git glib/tests/cond: Fix race condition The producer could push two values with the first one being lost. Fix this by blocking the producer until a consumer reads. --- diff --git a/glib/tests/cond.c b/glib/tests/cond.c index 4046ab0..f2ea6b0 100644 --- a/glib/tests/cond.c +++ b/glib/tests/cond.c @@ -33,6 +33,8 @@ static void push_value (gint value) { g_mutex_lock (&mutex); + while (next != 0) + g_cond_wait (&cond, &mutex); next = value; if (g_test_verbose ()) g_print ("Thread %p producing next value: %d\n", g_thread_self (), value); @@ -57,6 +59,7 @@ pop_value (void) } value = next; next = 0; + g_cond_broadcast (&cond); if (g_test_verbose ()) g_print ("Thread %p consuming value %d\n", g_thread_self (), value); g_mutex_unlock (&mutex); @@ -76,11 +79,9 @@ produce_values (gpointer data) { total += i; push_value (i); - g_usleep (1000); } push_value (-1); - g_usleep (1000); push_value (-1); if (g_test_verbose ())