mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-26 07:26:15 +01:00
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.
This commit is contained in:
parent
9115dd0a7c
commit
ff8f37ac05
@ -33,6 +33,8 @@ static void
|
|||||||
push_value (gint value)
|
push_value (gint value)
|
||||||
{
|
{
|
||||||
g_mutex_lock (&mutex);
|
g_mutex_lock (&mutex);
|
||||||
|
while (next != 0)
|
||||||
|
g_cond_wait (&cond, &mutex);
|
||||||
next = value;
|
next = value;
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_print ("Thread %p producing next value: %d\n", g_thread_self (), value);
|
g_print ("Thread %p producing next value: %d\n", g_thread_self (), value);
|
||||||
@ -57,6 +59,7 @@ pop_value (void)
|
|||||||
}
|
}
|
||||||
value = next;
|
value = next;
|
||||||
next = 0;
|
next = 0;
|
||||||
|
g_cond_broadcast (&cond);
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_print ("Thread %p consuming value %d\n", g_thread_self (), value);
|
g_print ("Thread %p consuming value %d\n", g_thread_self (), value);
|
||||||
g_mutex_unlock (&mutex);
|
g_mutex_unlock (&mutex);
|
||||||
@ -76,11 +79,9 @@ produce_values (gpointer data)
|
|||||||
{
|
{
|
||||||
total += i;
|
total += i;
|
||||||
push_value (i);
|
push_value (i);
|
||||||
g_usleep (1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
push_value (-1);
|
push_value (-1);
|
||||||
g_usleep (1000);
|
|
||||||
push_value (-1);
|
push_value (-1);
|
||||||
|
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
|
Loading…
Reference in New Issue
Block a user