source: trunk/third/glib2/tests/threadpool-test.c @ 18159

Revision 18159, 1.4 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18158, which included commits to RCS files with non-trunk default branches.
Line 
1#undef G_DISABLE_ASSERT
2#undef G_LOG_DOMAIN
3
4#include <glib.h>
5
6#define RUNS 100
7
8G_LOCK_DEFINE_STATIC (thread_counter);
9gulong abs_thread_counter;
10gulong running_thread_counter;
11
12void
13thread_pool_func (gpointer a, gpointer b)
14{
15  G_LOCK (thread_counter);
16  abs_thread_counter++;
17  running_thread_counter++;
18  G_UNLOCK (thread_counter);
19
20  g_usleep (g_random_int_range (0, 4000));
21
22  G_LOCK (thread_counter);
23  running_thread_counter--;
24  G_UNLOCK (thread_counter);
25}
26
27int
28main (int   argc,
29      char *argv[])
30{
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);
37 
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);
41
42  for (i = 0; i < RUNS; i++)
43    {
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);
47    }
48 
49  g_thread_pool_free (pool1, FALSE, TRUE);
50  g_thread_pool_free (pool2, FALSE, TRUE);
51  g_thread_pool_free (pool3, FALSE, TRUE);
52
53  g_assert (RUNS * 3 == abs_thread_counter);
54  g_assert (running_thread_counter == 0); 
55#endif
56  return 0;
57}
Note: See TracBrowser for help on using the repository browser.