Updated.
[glib.git] / tests / threadpool-test.c
blob4774840083aebc903bbb99fb0837b8d2698fe036
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <glib.h>
6 #define RUNS 100
8 G_LOCK_DEFINE_STATIC (thread_counter);
9 gulong abs_thread_counter;
10 gulong running_thread_counter;
12 void
13 thread_pool_func (gpointer a, gpointer b)
15 G_LOCK (thread_counter);
16 abs_thread_counter++;
17 running_thread_counter++;
18 G_UNLOCK (thread_counter);
20 g_usleep (g_random_int_range (0, 4000));
22 G_LOCK (thread_counter);
23 running_thread_counter--;
24 G_UNLOCK (thread_counter);
27 int
28 main (int argc,
29 char *argv[])
31 /* Only run the test, if threads are enabled and a default thread
32 implementation is available */
33 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
34 GThreadPool *pool1, *pool2, *pool3;
35 guint i;
36 g_thread_init (NULL);
38 pool1 = g_thread_pool_new (thread_pool_func, NULL, 3, FALSE, NULL);
39 pool2 = g_thread_pool_new (thread_pool_func, NULL, 5, TRUE, NULL);
40 pool3 = g_thread_pool_new (thread_pool_func, NULL, 7, TRUE, NULL);
42 for (i = 0; i < RUNS; i++)
44 g_thread_pool_push (pool1, GUINT_TO_POINTER (1), NULL);
45 g_thread_pool_push (pool2, GUINT_TO_POINTER (1), NULL);
46 g_thread_pool_push (pool3, GUINT_TO_POINTER (1), NULL);
49 g_thread_pool_free (pool1, FALSE, TRUE);
50 g_thread_pool_free (pool2, FALSE, TRUE);
51 g_thread_pool_free (pool3, FALSE, TRUE);
53 g_assert (RUNS * 3 == abs_thread_counter);
54 g_assert (running_thread_counter == 0);
55 #endif
56 return 0;