目次
1. プッシュまたはポップ スレッドのキャンセル処理ルーチン
1. プッシュまたはポップ スレッドのキャンセル処理ルーチン
1.理由
スレッドはいつでもミューテックスやセマフォなどのリソースを持っている可能性があるため、一度キャンセルすると他のスレッドでデッドロックが発生する可能性があるため、実際にスレッドがキャンセルされる可能性がある場合は、キャンセルする前に次の API を使用する必要があります。キャンセル要求の「処理ルーチン」を作成し、これらのルーチンが保持しているリソースを自動的に解放できるようにします。
2. 関連する API
3.コード
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void test(void * arg )
{
printf("收到取消请求, 参数arg : %d \n" , *(int *)arg);
}
void * func (void * arg)
{
int * p = calloc(1,4);
*p = 1024 ;
// 压入取消处理函数
pthread_cleanup_push( test , p );
while(1)
{
printf("这里是func线程,线程ID :%ld \n" , pthread_self() );
sleep(2);
break ;
}
//线程退出处理函数弹出, 0 不执行该函数 , 非0 则执行该处理函数
pthread_cleanup_pop( 0 );
// 退出本线程并设置返回值的地址(返回了num 的地址)
pthread_exit((void *)p); //返回的内存地址应该时一个堆空间
}
int main(int argc, char const *argv[])
{
// 创建线程
pthread_t t_id = -1 ;
pthread_create( &t_id , //新线程ID号
NULL , // 线程属性, NULL 默认属性
func, // 线程需要执行的例程(新线程需要执行的任务《函数》)
NULL ); // 线程的参数
printf("t_id : %ld\n" , t_id) ;
printf("这里是主函数,线程ID :%ld \n" , pthread_self() );
int * retval ;
int ret_val = 0 ;
sleep(1);
// pthread_cancel( t_id );
// 阻塞等待接合线程
printf("等待 function 线程退出:\n");
if( ret_val = pthread_join( t_id , (void*)&retval))
{
fprintf(stderr , "接合失败:%s\n" , strerror(ret_val));
}
printf("结合线程成功, 退出值为:%d\n" , *retval);
return 0;
}