Re: bug in thread-test.c



Hi Miroslaw ,

> 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.

Ok, this time with patch.

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

Index: thread-test.c
===================================================================
RCS file: /cvs/gnome/glib/tests/thread-test.c,v
retrieving revision 1.8
diff -p -u -b -B -r1.8 thread-test.c
--- thread-test.c	2001/05/18 08:44:57	1.8
+++ thread-test.c	2002/02/12 13:42:26
@@ -4,6 +4,7 @@
 
 static GMutex* test_g_mutex_mutex = NULL;
 static guint test_g_mutex_int = 0;
+static gboolean test_g_mutex_thread_ready;
 G_LOCK_DEFINE_STATIC (test_g_mutex);
 
 static gpointer
@@ -12,6 +13,7 @@ test_g_mutex_thread (gpointer data)
   g_assert (GPOINTER_TO_INT (data) == 42);
   g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
   g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
+  test_g_mutex_thread_ready = TRUE;
   g_mutex_lock (test_g_mutex_mutex);
   g_assert (test_g_mutex_int == 42);
   g_mutex_unlock (test_g_mutex_mutex);
@@ -27,9 +29,13 @@ test_g_mutex (void)
 
   g_assert (g_mutex_trylock (test_g_mutex_mutex));
   g_assert (G_TRYLOCK (test_g_mutex));
+  test_g_mutex_thread_ready = FALSE;
   thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
 			    TRUE, NULL);
-  g_usleep (G_USEC_PER_SEC);
+  /* This busy wait is only for testing purposes and not an example of
+   * good code!*/
+  while (!test_g_mutex_thread_ready)
+    g_usleep (G_USEC_PER_SEC / 5);
   test_g_mutex_int = 42;
   G_UNLOCK (test_g_mutex);
   g_mutex_unlock (test_g_mutex_mutex);
@@ -41,6 +47,7 @@ test_g_mutex (void)
 
 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
 static guint test_g_static_rec_mutex_int = 0;
+static gboolean test_g_static_rec_mutex_thread_ready;
 
 static gpointer
 test_g_static_rec_mutex_thread (gpointer data)
@@ -48,6 +55,7 @@ test_g_static_rec_mutex_thread (gpointer
   g_assert (GPOINTER_TO_INT (data) == 42);
   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
 	    == FALSE);
+  test_g_static_rec_mutex_thread_ready = TRUE;
   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
   g_assert (test_g_static_rec_mutex_int == 42);
@@ -66,11 +74,15 @@ test_g_static_rec_mutex (void)
   GThread *thread;
 
   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
+  test_g_static_rec_mutex_thread_ready = FALSE;
   thread = g_thread_create (test_g_static_rec_mutex_thread, 
 			    GINT_TO_POINTER (42), TRUE, NULL);
-  g_usleep (G_USEC_PER_SEC);
+  /* This busy wait is only for testing purposes and not an example of
+   * good code!*/
+  while (!test_g_static_rec_mutex_thread_ready)
+    g_usleep (G_USEC_PER_SEC / 5);
+
   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
-  g_usleep (G_USEC_PER_SEC);
   test_g_static_rec_mutex_int = 41;
   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
   test_g_static_rec_mutex_int = 42;  


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