static void test_thread_idle_time () { guint limit = 50; guint interval = 10000; gint i; idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func, NULL, MAX_THREADS, FALSE, NULL); g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); g_thread_pool_set_max_idle_time (interval); g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS); g_assert (g_thread_pool_get_max_idle_time () == interval); for (i = 0; i < limit; i++) { g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL); DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, " "number of threads:%d, unprocessed:%d", i, g_thread_pool_get_num_threads (idle_pool), g_thread_pool_unprocessed (idle_pool))); } g_timeout_add ((interval - 1000), test_thread_idle_timeout, GUINT_TO_POINTER (interval)); }
static void test_thread_functions (void) { gint max_unused_threads; guint max_idle_time; /* This function attempts to call functions which don't need a * threadpool to operate to make sure no uninitialised pointers * accessed and no errors occur. */ max_unused_threads = 3; DEBUG_MSG (("[funcs] Setting max unused threads to %d", max_unused_threads)); g_thread_pool_set_max_unused_threads (max_unused_threads); DEBUG_MSG (("[funcs] Getting max unused threads = %d", g_thread_pool_get_max_unused_threads ())); g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads); DEBUG_MSG (("[funcs] Getting num unused threads = %d", g_thread_pool_get_num_unused_threads ())); g_assert (g_thread_pool_get_num_unused_threads () == 0); DEBUG_MSG (("[funcs] Stopping unused threads")); g_thread_pool_stop_unused_threads (); max_idle_time = 10 * G_USEC_PER_SEC; DEBUG_MSG (("[funcs] Setting max idle time to %d", max_idle_time)); g_thread_pool_set_max_idle_time (max_idle_time); DEBUG_MSG (("[funcs] Getting max idle time = %d", g_thread_pool_get_max_idle_time ())); g_assert (g_thread_pool_get_max_idle_time () == max_idle_time); DEBUG_MSG (("[funcs] Setting max idle time to 0")); g_thread_pool_set_max_idle_time (0); DEBUG_MSG (("[funcs] Getting max idle time = %d", g_thread_pool_get_max_idle_time ())); g_assert (g_thread_pool_get_max_idle_time () == 0); }
void engine_setupWorkerThreads(Engine* engine, gint nWorkerThreads) { MAGIC_ASSERT(engine); if(nWorkerThreads > 0) { /* we need some workers, create a thread pool */ GError *error = NULL; engine->workerPool = g_thread_pool_new((GFunc)worker_executeEvent, engine, nWorkerThreads, FALSE, &error); if (!engine->workerPool) { error("thread pool failed: %s", error->message); g_error_free(error); } guint interval = g_thread_pool_get_max_idle_time(); info("Threads are stopped after %lu milliseconds", interval); } }