Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

线程pthread_join和分离式线程

Posted on 2012-04-23 10:26 Prayer 阅读(1328) 评论(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() 将不会执行,pthread_join使一个线程等待另一个线程结束。
 //则而这个线程运行又非常快,线程处理函数得不到执行
 //它很可能在pthread_create函数返回之前就终止了


/* 方法2 多线程创建分离式线程 注意如果设置一个线程为分离线程,
 而这个线程运行又非常快,它很可能在pthread_create函数返回之前就终止了
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");
  
}

 




只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理