- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void *child1(void *arg)
{
int i = 0;
printf("come in thread_id=%u\n",pthread_self());
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
while(1){
//printf("Iam comin while my pthread_id=%u\ti=%d\n",pthread_self(),i++);
printf("i=%d\n",i++);
}
//return (void *)6;
}
int main( int argc, char *argv[] )
{
pthread_t pthread1;
int ret;
pthread_create(&pthread1,NULL,(void *)child1,NULL);
sleep(1);
ret = pthread_cancel(pthread1);
if (0 != ret){
perror("call pthread_cancel");
return -1;
}
ret = pthread_join(pthread1, NULL);
if (0 != ret){
perror("call pthread_join");
return -1;
}
printf("over main!return\n");
return 0;
}
在main函数中设置了pthread_cancel(),但在child1中没有设置取消点,为什么不会死循环?而是执行到一定数值就退出?
[ 本帖最后由 foxwolf_ym 于 2008-7-16 17:53 编辑 ] |
|