glib Thread Pools 测试

#include<glib.h>

static void test_function(gpointer data, gpointer user_data)
{
    int i;
    i = GPOINTER_TO_INT(data);
    g_print("test function %d\n",i);
}

int main(void)
{
    GThreadPool *pool = NULL;
    GError *error = NULL;

    int i;

    if (!g_thread_supported())
    {
        g_print("Not support g_thread!\n");
        return 0;
    }

    //从2.32版开始,GLib线程系统将在程序开始时自动初始化
    //g_thread_init (NULL);
    pool = g_thread_pool_new(test_function, NULL, -1, FALSE, &error);
    if(pool == NULL) {
        g_print("Can not create thread!\n");
        return 0;
    }

    for(i = 0; i < 10 ; i++)
    {
        g_thread_pool_push(pool, GINT_TO_POINTER(i + 1), NULL);
    }

    g_thread_pool_free (pool, FALSE, TRUE);

    return 0;
}
$ ./threadpool-test.exe
test function 2
test function 1
test function 3
test function 7
test function 4
test function 6
test function 5
test function 8
test function 9
test function 10

 

posted @ 2021-03-29 15:00  阿C  阅读(159)  评论(0编辑  收藏  举报