念两句诗

千古斜阳,无处问长安。
【宋代】张舜民《江神子·癸亥陈和叔会于赏心亭》
随笔 - 12,  文章 - 1,  评论 - 0,  阅读 - 2446

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

posted on   昔九  阅读(577)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示