有关多线程的问题,求教
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
已放弃
不明白,为什么会这样的结果,求高手解答!!!!!
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
已放弃
不明白,为什么会这样的结果,求高手解答!!!!!
...全文
相关推荐
求教C++等等一系列的问题
这是一个值得人深思的事情,希望莘莘学子能都好好的考虑一下这些内容的背后
QT中sqlite多线程操作4个注意问题
总结了一下Qt中sqlite多线程操作遇到的几个问题,希望能对有需要的朋友一点帮助
个人总结40个Java多线程面试问题和答案
个人总结40个Java多线程面试问题和答案,很全面,让你不再担心多线程的面试问题。
求教类似SUMIF;COUNTIF的条件函数的写法???
求教类似SUMIF;COUNTIF的条件函数的写法???
Python tkinter 多线程 哲学家进餐问题
Python tkinter 多线程 哲学家进餐问题 网上找了好久都没有自己满意的代码,于是自己学了一个,不过这只是个雏形,要想变成你满意的程序,需要靓男靓女们自己的努力啦! 废话不多说,直接上代码! from tkinter import * from PIL import Image,ImageTk import threading from time import sleep import os, random """首先导入必要的模块,由于涉及到导入非gif的图片文件,就要用到PIL库""" """接下来开始写哲学家这个类""" class Philosopher(threading.


