您的位置:首页 > 其它

pthread_create和pthread_detach, pthread_cancel的使用

2015-04-03 16:42 363 查看

pthread_create和pthread_detach, pthread_cancel的使用

int input_stop(void) {
DBG("will cancel input thread\n");
pthread_cancel(worker);   //发送终止信号给线程,如果成功则返回0,否则为非0值。发送成功并不意味着thread会终止
return 0;
}

int input_run(void) {
pglobal->buf = malloc(256*1024);
if (pglobal->buf == NULL) {
fprintf(stderr, "could not allocate memory\n");
exit(1);
}

if( pthread_create(&worker, 0, worker_thread, NULL) != 0) { // 创建一个worker线程
free(pglobal->buf);
fprintf(stderr, "could not start worker thread\n");
exit(EXIT_FAILURE);
}
// 状态设置为detached,则该线程运行结束后会自动释放所有资源
pthread_detach(worker); // 非阻塞,可立即返回,和pthread_join比较,pthread_detach为非阻塞,

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: