- 論壇徽章:
- 0
|
回復(fù) 4# foolishx
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void *thread(void* m)
{
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL); //??????
// pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //??????
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); //??????
unsigned long t,id;
t = *(unsigned long *)m;
while(1)
{
printf("thread %ld\n",pthread_self()); //??????
sleep(3);
// pthread_testcancel();
}
}
int main()
{
pthread_t t[2];
pthread_attr_t at;
int ret, i;
for(i = 0; i < 2;i++)
{
ret = pthread_create(&t,NULL,thread,&i);
if(ret!=0)
{
printf ("Create pthread error!n");
exit (1);
}
printf("%ld\n", t);
}
while(1){
// pthread_cancel(t[0]); //10???,???????. t??????
// pthread_cancel(t[1]); //10???,???????. t??????
sleep(10);
pthread_cancel(t[1]); //10???,???????. t??????
pthread_cancel(t[0]); //10???,???????. t??????
}
}
現(xiàn)在感覺很奇怪
測試代碼如上, 如果我給線程1,2都發(fā)送了cancel信號,但是由于線程中沒有cancel點,所以一直卡在,但為什么printf的都沒有打了呢?
如果我在線程1,2都加上pthread_testcancel(),線程沒有正常退出,線程也沒有打印printf的內(nèi)容
如果主線程只給一個線程發(fā)送cancel信號,則無論有沒有pthread_testcancel() 線程都會退出一個。。。。
|
|