Posted on 2012-04-23 10:26
Prayer 閱讀(1357)
評論(0) 編輯 收藏 引用 所屬分類:
LINUX/UNIX/AIX
http://blog.csdn.net/qq515383106/article/details/7419731
CALLBACK_POINT_FUN pC = NULL;
void call_fun(CALLBACK_POINT_FUN fun)
{
pC = fun;
//方法1 單線程都用這樣方法
pthread_t sh1;
int ret;
ret = pthread_create(&sh1, NULL, (void *)callback_thread, NULL);
if(ret)
printf("create thread is fail\n");
pthread_join(sh1, NULL); //重要
//若不加入這句。callback_thread() 將不會執(zhí)行,pthread_join使一個線程等待另一個線程結(jié)束。
//則而這個線程運行又非常快,線程處理函數(shù)得不到執(zhí)行
//它很可能在pthread_create函數(shù)返回之前就終止了
/* 方法2 多線程創(chuàng)建分離式線程 注意如果設(shè)置一個線程為分離線程,
而這個線程運行又非常快,它很可能在pthread_create函數(shù)返回之前就終止了
int ret ;
printf("call_fun\n");
pthread_t sh;
pthread_attr_t attrca;
pthread_attr_init (&attrca);//分離式線程
pthread_attr_setdetachstate (&attrca, PTHREAD_CREATE_DETACHED);
if(ret = pthread_create(&sh, NULL, (void *)callback_thread, NULL))
{
printf("create thread failed\n");
}
usleep(1000); //讓主線程休息一會,等到子線程完成
*/
}
void callback_thread()
{
printf("the thread is run\n");
}