pthread_exit 表示线程结束,退出当前线程。
在main函数结尾时使用return 0 和使用pthread_exit有什么区别呢
1.使用return 0;
1 #include "windows.h" 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define MAX_NUM 4 6 7 typedef struct thread_info 8 { 9 int id; 10 string name; 11 } thread_info; 12 13 //void*只是表示可以传入任意类型的指针,返回值也可以是任意类型的指针 14 void *print_hello(void *arg) 15 { 16 Sleep(2000); 17 thread_info *ifo = (thread_info *)arg; 18 cout << "child thread" << endl; 19 // int id = ifo->id; 20 // string name = ifo->name; 21 // cout << "ID: " << id << " NAME" << name << endl; 22 } 23 int main() 24 { 25 thread_info info[MAX_NUM]; 26 pthread_t tids[MAX_NUM]; 27 for (int i = 0; i < MAX_NUM; i++) 28 { 29 info[i].id = i; 30 info[i].name = " PETER" + to_string(i); 31 cout << "create thread " << i << endl; 32 int ret = pthread_create(&tids[i], NULL, print_hello, (void *)&info[i]); 33 if (ret){ 34 cout << "something wrong during create thread" << endl; 35 } 36 } 37 //pthread_exit(NULL); 38 return 0; 39 }
运行结果:
可见子线程随着主线程的退出也销毁掉,没有打印输出
2.使用pthread_exit(NULL)
1 #include "windows.h" 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define MAX_NUM 4 6 7 typedef struct thread_info 8 { 9 int id; 10 string name; 11 } thread_info; 12 13 //void*只是表示可以传入任意类型的指针,返回值也可以是任意类型的指针 14 void *print_hello(void *arg) 15 { 16 Sleep(2000); 17 thread_info *ifo = (thread_info *)arg; 18 cout << "child thread" << endl; 19 // int id = ifo->id; 20 // string name = ifo->name; 21 // cout << "ID: " << id << " NAME" << name << endl; 22 } 23 int main() 24 { 25 thread_info info[MAX_NUM]; 26 pthread_t tids[MAX_NUM]; 27 for (int i = 0; i < MAX_NUM; i++) 28 { 29 info[i].id = i; 30 info[i].name = " PETER" + to_string(i); 31 cout << "create thread " << i << endl; 32 int ret = pthread_create(&tids[i], NULL, print_hello, (void *)&info[i]); 33 if (ret){ 34 cout << "something wrong during create thread" << endl; 35 } 36 } 37 pthread_exit(NULL); 38 //return 0; 39 }
运行结果:
由此可以知道pthread_exit退出主线程后其子线程依旧运行。
3.使用pthread_exit 是否会销毁进程资源
1 #include "windows.h" 2 #include <bits/stdc++.h> 3 using namespace std; 4 #define MAX_NUM 4 5 6 typedef struct thread_info 7 { 8 int id; 9 string name; 10 } thread_info; 11 12 void *print_hello(void *arg) //void*只是表示可以传入任意类型的指针,返回值也可以是任意类型的指针 13 { 14 Sleep(2000); 15 thread_info *ifo = (thread_info *)arg; 16 cout << "child thread" << endl; 17 int id = ifo->id; 18 string name = ifo->name; 19 cout << "ID: " << id << " NAME" << name << endl; 20 } 21 int main() 22 { 23 thread_info info[MAX_NUM]; 24 pthread_t tids[MAX_NUM]; 25 for (int i = 0; i < MAX_NUM; i++) 26 { 27 info[i].id = i; 28 info[i].name = " PETER" + to_string(i); 29 cout << "create thread " << i << endl; 30 int ret = pthread_create(&tids[i], NULL, print_hello, (void *)&info[i]); 31 if (ret){ 32 cout << "something wrong during create thread" << endl; 33 } 34 } 35 pthread_exit(NULL); 36 //return 0; 37 }
如图可以看到主线程退出前,结构体数组info中存储着ID和name,
主线程通过pthread_exit(NULL) 退出后,主线程中的变量info将销毁不能访问。
4.主线程退出前等待子线程
1 for (int i = 0; i < MAX_NUM; i++){ 2 int ret=pthread_join(tids[i], &status); 3 //int ret=pthread_detach(tids[i]); 4 }
以上为Windows11上的测试,如有问题欢迎批评指正。
2022-03-24 16:39:02
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!