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
已放弃
不明白,为什么会这样的结果,求高手解答!!!!!