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;
}