线程分离pthread_detach()中的return()和pthread_exit()
2024-08-25 01:04:58
#include <stdio.h>
#include <pthread.h> pthread_t tid[];
void *func_2(void *arg);
void *func_1(void *arg)
{ //为函数func_2创建线程
printf("Starting func_1\n");
pthread_create(&tid[], NULL, func_2, NULL);
//tid[0]将自己挂起,等待线程tid[1]的结束
pthread_join(tid[], NULL);
} void *func_2(void *arg)
{
printf("Starting func_2\n");
sleep();
pthread_exit(NULL);
} int main()
{ //为函数func_1创建线程并将其分离
pthread_create(&tid[], NULL, func_1, NULL);
// pthread_detach(tid[0], NULL); //分离
pthread_detach(tid[]); //分离
//主线程不退出,但这两个现成的资源都可以释放掉
// return 0;
pthread_exit(NULL);
}
在main()函数中,如果采用return 0,则没有任何输出就退出了;而如果采用的是pthread_exit(NULL),则打印出消息
最新文章
- .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整-控制反转和依赖注入的使用
- VS调试程序时一闪而过的问题-解决方法(网上搜集)
- [原创]使用Gradle的插件EnIDE build的时候,输出的中文总是乱码。
- salesforce 零基础开发入门学习(十四)salesforce中工厂模式的运用
- 2014.10.09 Andrew 学习 WPF(刘铁锰) 笔记分享
- SSH WebShell: SSH在线WEB管理器安装教程 - VPS管理百科
- MySQL ProxySQL读写分离实践
- [转] .NET领域驱动设计—看DDD是如何运用设计模式颠覆传统架构
- shell 安装使用VIM
- Kubernetes+Docker的云平台在CentOS7系统上的安装
- Net dll组件版本兼容问题
- Syntactic and Semantic Errors
- nwjs 实现的 分块上传
- Java I/O 应用程序设计
- Python 爬验证码
- Sql Server 2008 清理日志文件
- 基本SQL命令
- android 9 patch
- xcode恢复语法高亮
- JavaScript工作机制:V8 引擎内部机制及如何编写优化代码的5个诀窍