Search code examples
cmultithreadingthreadpoolglib

Is there a function to join threads from thread pool in glib?


Here is the sample program.

#include<glib.h>
void call_me(gpointer data, gpointer user_data)
{
    g_usleep(1);
    g_print("I am called!\n");
}
int main(int argc, char *argv[])
{
    GThreadPool* th_pool = g_thread_pool_new((GFunc)call_me, NULL, 200, TRUE, NULL);
    for(int i = 0; i < 10; ++i){
        g_thread_pool_push(th_pool, (void*)10, NULL);
    }
    return 0;
}

I think the solution is to join the threads in the thread pool because the problem is main thread returns before all the threads get their time. But, I could not find a function in Glib. If there is other way to solve the problem, could you please highlight them?


Solution

  • Call g_thread_pool_free (th_pool, TRUE, TRUE). It will wait for all pending tasks to finish running, and then free the memory used by the GThreadPool.