我面临着一个与pthread_cancel相关的问题。请参考下面的代码:
void* func(void *arg)
{
while(1)
{
sleep(2);
}
}
#include<stdlib.h>
#include <stdio.h>
#include <pthread.h>
int main()
{
void *status;
pthread_t thr_Var;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
pthread_create(&thr_Var,NULL,func,NULL);
pthread_cancel(thr_Var);
pthread_join(thr_Var,&status);
return 0;
}我的怀疑是,即使我禁用了取消状态,pthread_cancel仍然在工作,线程正在终止。任何帮助我们都将不胜感激。
发布于 2012-10-23 22:27:00
pthread_setcancelstate设置调用线程的可取消类型,即本例中的主线程。因此,如果您希望使新创建的线程不可取消,则应该从该线程的上下文中调用该函数。
请参阅man 3 pthread_setcancelstate
注意,虽然Linux pthread实现允许空oldstate指针,但是POSIX并没有指定空指针,所以最好为oldsate提供一个指针。
https://stackoverflow.com/questions/13032659
复制相似问题