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; }
相关文章推荐
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_create创建线程后必须使用join或detach释放线程资源[内存泄漏]
- 2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_create()使用detach分离后仍有内存泄漏
- pthread_create创建线程后必须使用join或detach释放线程资源[内存泄漏]
- C++的类中使用pthread_create()
- 解决使用pthread_create函数造成的内存泄露
- pthread_cancel 使用方法2
- 使用 pthread_testcancel 作为pthread_cancel 时 线程的退出点函数
- 使用 pthread_create存在的问题
- pthread_create后没有detach导致内存持续增长
- 一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- 解决了一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- 使用pthread_create时参数的传递
- 如何不使用pthread_cancel而杀死线程
- 解决了一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- pthread_cancel 使用方法1
- linux 下线程使用(使用pthread_create)
- 使用pthread_create创建大量线程时,创建线程失败,Resource temporarily unavailable的解决办法