1
回答
请达人帮我分析下glib 线程池问题
终于搞明白,存储TCO原来是这样算的>>>   

glibtest.c

#include <glib.h>

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

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


gint i,gthreadcount;
GMutex *mutex;

if(!g_thread_supported())
{
	g_thread_init(NULL);
}

mutex = g_mutex_new();
pool=g_thread_pool_new(test_function,NULL,-1,FALSE,&error);
if(pool == NULL)
{
g_print("can not create thread");
}
gthreadcount = g_thread_pool_get_num_threads(pool);
        
g_print("%d\n",gthreadcount);


  g_mutex_lock(mutex);
      
        for(i = 1; i < 10 ; i++)
        {
              g_thread_pool_push(pool, (gpointer *)i , &error);
			 
        }
   g_mutex_unlock(mutex);
}

 

 

上面这段代码用编译命令:gcc  glibtest.c -o glibtest  `pkg-config --cflags --libs gthread-2.0  glib-2.0`   编译

执行后控制台打印出0  ,不知道为什么  ,我是在rhel 5下编译的

举报
coolge
发帖于6年前 1回/436阅
顶部