- 论坛徽章:
- 0
|
今天发现win2000 + vmware + redhat9下,
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&nOldType);
没有起作用,也就是说就算设置了PTHREAD_CANCEL_DISABLE,也会被pthread_cancel 函数cancel掉
测试程序如下
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <pthread.h>
- pthread_t pid1;
- void func_1(void)
- {
- int i ;
- int nOldType;
- int nRet = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&nOldType);
- printf("nRet = %d\n",nRet);
- if ( nOldType == PTHREAD_CANCEL_ENABLE)
- printf("old type is enable\n");
- nRet = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&nOldType);
- if ( nOldType == PTHREAD_CANCEL_DISABLE)
- printf("old type is disable\n");
- for(i = 1 ;i<60;i++)
- {
- printf("%d\n",i);
- sleep(1);
- }
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
- for(i = 1 ;i < 10; i++ )
- {
- printf("test %d\n",i);
- sleep(1);
- }
- return ;
- }
- void * thread_func_1(void * arg)
- {
- func_1();
- return NULL;
- }
- int main(int argc,char ** argv)
- {
- pthread_create(&pid1,NULL,thread_func_1,NULL);
- sleep(10);
- pthread_cancel(pid1);
- printf("cancel thread1\n");
- pthread_join(pid1,NULL);
- return 0;
- }
复制代码
在IBM的AIX下,
for(i = 1 ;i<60;i++)
{
printf("%d\n",i);
sleep(1);
}
会被全部执行完,也就是说执行printf("%d\n",i)60次
而在win2000 + vmware + redhat9 下 只会执行10次printf,也就是10次后受到cancel信息,但是之前设置的
int nRet = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&nOldType);
没有起作用。
有兴趣的朋友们在自己环境下编译测试下看看,然后把结果告诉大家。 |
|