有关多线程的问题,求教

liuxialong liuxialong 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
1 #include <iostream>
2 #include <unistd.h>
3 #include <pthread.h>
4
5 using std::cout;

6 using std::endl;
7
8 static int num=0;
9
10 void clean_up(void *)
11 {
12 num++;
13 }
14
15 void *func(void *)
16 {
17 pthread_cleanup_push(clean_up, NULL);
18 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
19 while(1)
20 {
21 int a=0;
22 }
23 pthread_cleanup_pop(0);
24 return (void *)1;
25 }
26
27 int main(void)
28 {
29 pthread_t tid[1024];
30 for(int i=0; i<1024; i++)
31 {
32 pthread_create(&tid[i], NULL, func, NULL);
33 }
34 sleep(10);
35 for(int i=0; i<1024; i++)
36 {
37 pthread_cancel(tid[i]);
38 }
39 sleep(10);
40 cout<<num<<endl;
41 return 0;
42
43 }

程序执行的结果却是:
terminate called without an active exception
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called without an active exception
terminate called recursively
已放弃


不明白,为什么会这样的结果,求高手解答!!!!!

...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
liuxialong
Linux/Unix社区 23,224 成员 74,535 内容