linux线程系列(5)线程清理和控制函数
来源:互联网 发布:手机淘宝点购买没反应 编辑:程序博客网 时间:2024/06/05 10:03
线程清理函数有两个push和pop函数:
#include <pthread.h>void pthread_cleanup_push(void (*rtn)(void *), void* arg);void pthread_cleanup_pop(int execute);返回: 成功返回0,否则返回错误编号参数rtn:清理函数指针arg:调用清理函数传递的参数execute:值1时执行线程清理函数, 值0时不执行线程清理函数。这两个函数在子线程中被调用,必须成对出现(有push,必须有pop),当子线程结束的时候,如果execute的值为非零,push函数中的清理函数就会被执行。
子线程结束包括子线程调用return、调用pthread_exit、响应取消请求(其它线程调用pthread_canel函数,取消该线程)等。
由于采用了栈的机制,先push的清理函数后执行,如下面代码,首先打印second clean func,再打印first clean func:
#include <pthread.h>#include <stdlib.h>#include <stdio.h>//定义线程清理函数void clean_fun(void &arg){ char *s = (char*)arg; printf("clean_fun: %s\n", s);}void* th_fun(void *arg){ int execute = (int)arg; pthread_cleanup_push(clean_fun, "first clean func"); pthread_cleanup_push(clean_fun, "second clean func"); printf("thread running %lx\n", pthread_self()); pthread_clean_pop(execute); pthread_clean_pop(execute); return (void*)0;}int main(){ int err; pthread_t th1, th2; if((err = pthread_create(&th2, NULL, th_fun, (void*)1)) != 0) { perror("pthread create error"); return -1; } pthread_join(th1, NULL); printf("th1(%lx) finished\n", th1); if((err = pthread_create(&th2, NULL, th_fun, (void*)1)) != 0) { perror("pthread create error"); return -1; } pthread_join(th2, NULL); printf("th2(%lx) finished\n", th2); return 0;}
阅读全文
0 0
- linux线程系列(5)线程清理和控制函数
- Linux Advance--线程清理处理函数
- Linux线程与线程控制函数-笔记
- POSIX线程清理函数
- 78-线程清理函数
- 线程清理函数
- 线程清理函数
- linux线程控制和通信
- linux线程控制和通信
- linux线程控制和通信
- 线程清理函数pthread_cleanup_push / pthread_cleanup_pop
- Linux线程(3): 线程清理处理程序
- linux网络编程之posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
- linux线程系列(3)线程创建
- linux线程系列(4)线程终止
- linux线程系列(1)线程的概念、线程和进程的关系
- 线程控制(linux C)
- linux线程控制&线程分离
- 在邻接矩阵中的DFS与BFS的实现
- Leetcode 360 Sort Transformed Array
- vim安装插件YouCompleteMe,出错,YouCompleteMe unavailable: requires Vim compiled with Python 2.x support
- 编译防火墙——C++的Pimpl惯用法解析
- socket学习
- linux线程系列(5)线程清理和控制函数
- SVM简单代码实现MATLAB
- CS224d Assignment1 答案, Part(3/4)
- C++ 将当前系统时间转换成标准格式的时间和时间戳
- KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data的解释
- hdu 4571 Travel in time 最短路+dp
- C# 方法重载
- Codeforces Round #416 (Div. 2) 题解
- 常见数学符号读法