[glib] glib/tests/cond: Fix race condition



commit ff8f37ac05490c67fc3be5e87ff5756f1a275da3
Author: Colin Walters <walters verbum org>
Date:   Thu May 23 01:07:13 2013 +0100

    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.

 glib/tests/cond.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
---
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 ())


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]