- 论坛徽章:
- 0
|
回复 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??????
}
}
现在感觉很奇怪
测试代码如上, 如果我给线程1,2都发送了cancel信号,但是由于线程中没有cancel点,所以一直卡在,但为什么printf的都没有打了呢?
如果我在线程1,2都加上pthread_testcancel(),线程没有正常退出,线程也没有打印printf的内容
如果主线程只给一个线程发送cancel信号,则无论有没有pthread_testcancel() 线程都会退出一个。。。。
|
|