你的理解不对。
首先,pthread_setcancelstate()函数只是改变本线程(注意是本线程)的cancel state。所以T1进入fun()函数,执行到pthread_setcancelstate()函数时,只是改变了T1本身的cancel state,并不能改变T2的cancel state。
第二,线程执行到pthread_testcancel()函数时,并不一定会马上取消(退出)。
先描述一下取消一个线程的过程:
1) 其他线程通过调用pthread_cancel()函数,向目标线程发送取消请求(cancellation request)。
2) 取消请求发出后,根据目标线程的cancel state来决定取消请求是否会到达目标线程:
a. 如果目标线程的cancel state是PTHREAD_CANCEL_ENABLE(默认),取消请求会到达目标线程。
b. 如果目标线程的cancel state是PTHREAD_CANCEL_DISABLE,取消请求会被放入队列。直到目标线程的cancel state变为PTHREAD_CANCEL_ENABLE,取消请求才会从队列里取出,发到目标线程。
3) 取消请求到达目标线程后,根据目标线程的cancel type来决定线程何时取消:
a. 如果目标线程的cancel type是PTHREAD_CANCEL_DEFERRED(默认),目标线程并不会马上取消,而是在执行下一条cancellation point的时候才会取消。有很多系统函数都是cancellation point,详细的列表可以在Linux上用man 7 pthreads查看。除了列出来的cancellation point,pthread_testcancel()也是一个cancellation point。就是说目标线程执行到pthread_testcancel()函数的时候,如果该线程收到过取消请求,而且它的cancel type是PTHREAD_CANCEL_DEFERRED,那么这个线程就会在这个函数里取消(退出),这个函数就不再返回了,目标线程也没有了。
b. 如果目标线程的cancel type是PTHREAD_CANCEL_ASYNCHRONOUS,目标线程会立即取消(这里的“立即”只是说目标线程不用等执行到属于cancellation point的函数的时候才会取消,它会在获得调度之后立即取消,因为内核调度会有延时,所以并不能保证时间上的“立即”)。
举个例子,说明一下这些与线程取消相关的函数的用法:
void thread_function(void *arg)。
/**
* 线程准备执行一些关键工作,在这个过程中不希望被取消。
* 所以先通过pthread_setcancelstate()将本线程的cancel state。
* 设为disabled。
*/
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);。
/* 执行关键工作 */
...
/**
* 关键工作执行完成,可以被取消。
* 通过pthread_setcancelstate()将本线程的cancel state。
* 设为enabled。
*/
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);。
/**
* 调用pthread_testcancel()函数,检查一下在cancel state。
* 为disabled状态的时候,是否有取消请求发送给本线程。
* 如果有的话就取消(退出)。
*/
pthread_testcancel();。
/**
* pthread_testcancel()返回了,表明之前没有取消请求发送给本线程,
* 继续其余的工作。
* 这时候如果有取消请求发送给本线程,会在下一次执行到。
* cancellation point的时候(例如sleep(), read(), write(), ...)时取消。
*/
...
/**
* 从这里开始,函数里不再包含cancellation point了。
* 如果收到取消请求,将无法取消。所以先把本线程的cancel type。
* 设为asynchronous,收到取消请求将立即取消。
*/
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);。
/* 不包含cancellation point的代码 */。
...
多线程退出有三种方式:(1)执行完成后隐式退出;(2)由线程本身显示调用pthread_exit函数退出;pthread_exit(void*retval);(3)被其他线程用pthread_cance函数终止:pthread_cance(pthread_tthread);用event来实现。在子线程中,在循环内检测event。while(!e.is_active()){}当退出循环体的时候,自然return返回。这样子线程会优雅的结束。注意:选用非等待的检测函数。pthread线程有两种状态,joinable(非分离)状态和detachable(分离)状态,默认为joinable。joinable:当线程函数自己返回退出或pthread_exit时都不会释放线程所用资源,包括栈,线程描述符等(有人说有8k多,未经验证)。detachable:线程结束时会自动释放资源。Linuxmanpagesaid:Whenajoinablethreadterminates,itsmemoryresources(threaddescriptorandstack)arenotdeallocateduntilanotherthreadperformspthread_joinonit.Therefore,pthread_joinmustbecalledonceforeachjoinablethreadcreatedtoavoidmemoryleaks.因此,joinable线程执行完后不使用pthread_join的话就会造成内存泄漏。解决法:1.//创建线程前设置PTHREAD_CREATE_DETACHED属性pthread_attr_tattr;pthread_tthread;pthread_attr_init(&attr);pthread_attr_setdetachstat(&attr,PTHREAD_CREATE_DETACHED);pthread_create(&thread,&attr,&thread_function,NULL);pthread_attr_destroy(&attr);2.当线程为joinable时,使用pthread_join来获取线程返回值,并释放资源。3.当线程为joinable时,也可在线程中调用pthread_detach(pthread_self());来分离自己。
执行完成后隐式退出
由线程本身显示调用pthread_exit 函数退出;pthread_exit (void * retval)。
被其他线程用pthread_cance函数终止:pthread_cance (pthread_t thread)。
解决办法:
// 创建线程前设置 PTHREAD_CREATE_DETACHED 属性。
pthread_attr_t attr;。
pthread_t thread;。
pthread_attr_init (&attr);。
pthread_attr_setdetachstat(&attr, PTHREAD_CREATE_DETACHED);。
pthread_create (&thread, &attr, &thread_function, NULL);。
pthread_attr_destroy (&attr);。
当线程为joinable时,使用pthread_join来获取线程返回值,并释放资源。
当线程为joinable时,也可在线程中调用 pthread_detach(pthread_self());。
这个是非常复杂的工程,在这问不靠谱。多线程程序没有几个星期的系统学习,靠网上“大佬”几句话就开始写,那是制造灾难。
我以前学多线程时,unix下借助的是APUE(Advanced Programming in Unix Environment),Windows下借助的是Visual C++技术内幕。现在可能有更多好书了,楼主可以去找找看。
把handle的地址作为线程参数传进去。
HANDLE handle; 。
hThread = CreateThread( NULL, 0, ThreadFunc, (LPVOID)&handle,0, &dwThreadId); 。
你ThreadFunc里面那个x就是了。