转自:
http://blog.csdn.net/hiflower/article/details/2198214
请问,我在用pthread_cancel函数取消一个线程时,但该线程并没有设置取消点,可是运行出来的结果却和理论值两样。因为我在线程中用了一个for(;;),照理该线程应该一直循环下去,可是该线程却可以退出。该程序如下:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fd;
char buf[40];
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void *search(void *arg)
{
int d=0;
for(;;) printf("d=%d/n",d++); //将此句的去掉,只留一个分号,线程就不能退出了。^_^
printf("/nend/n");
}
int main()
{
pthread_t tid;
pthread_create(&tid,NULL,search,NULL);
pthread_cancel(tid);
printf("the main end/n");
pthread_join(tid,NULL);
printf("the cancel prom is over/n");
}
只要按上述注释中的方法去掉 printf ,线程就不会退出,因此,我认为 printf 也是个取消点。