Re: bug in thread-test.c



Hi Miroslaw ,

> threre is a bug in glib-1.3.13/tests/thread-test.c
> 
>  9 static gpointer
> 10 test_g_mutex_thread (gpointer data)
> 11 {
> 12   g_assert (GPOINTER_TO_INT (data) == 42);
> 13   g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
> 14   g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
> 15   g_mutex_lock (test_g_mutex_mutex);
> 16   g_assert (test_g_mutex_int == 42);
> 17   g_mutex_unlock (test_g_mutex_mutex);
> 18
> 19   return GINT_TO_POINTER (41);
> 20 }
> 21
> 22 static void
> 23 test_g_mutex (void)
> 24 {
> 25   GThread *thread;
> 26   test_g_mutex_mutex = g_mutex_new ();
> 27
> 28   g_assert (g_mutex_trylock (test_g_mutex_mutex));
> 29   g_assert (G_TRYLOCK (test_g_mutex));
> 30   thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
> 31                             TRUE, NULL);
> 32   g_usleep (G_USEC_PER_SEC);
> 33   test_g_mutex_int = 42;
> 34   G_UNLOCK (test_g_mutex);
> 35   g_mutex_unlock (test_g_mutex_mutex);
> 36   g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
> 37   g_mutex_free (test_g_mutex_mutex);
> 38 }
>  
> line 32
> You can not expect that after G_USEC_PER_SEC the thread running
> test_g_mutex_thread() get executed. Sleep is the wrong solution for thread
> synchronization. If test_g_mutex_thread() is scheduled after
> line 35 the thest fails in line 13.
> You should use g_thread_join() instead.

While in theory you are right, I think in practice for a test this is
OK. It is to test, whether the locks work, in a real program of course
you shouldn't make such assumptions. gthread_join wouldn't solve this,
because for the test indeed I want to have both threads run concurrently
and I want test_g_mutex_thread to really return FALSE for lines 13 and
14 and to really hang in line 15. 

However this could as well be solved with busy wait, I agree, what do
you think about the appanded patch. It removes all g_usleep, that just
wait for a thread to start and run till it blocks, but leaves those
g_usleep in, which say: Wait sometime, if it breaks or not.

Bye,
Sebastian
-- 
Sebastian Wilhelmi
mailto:wilhelmi ira uka de
http://goethe.ira.uka.de/~wilhelmi





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