pthread——pthread_cleanup
来源:互联网 发布:积分记录软件 编辑:程序博客网 时间:2024/06/14 20:55
Pthread_cleanup用于注册线程清理函数,注册的清理函数将在线程被取消或者主动调用pthread_exit时被调用;
一个简单的示例:
#include <pthread.h>
#include <stdio.h>
// pthread_cleanup_push and pthread_cleanup_pop should be called in pairs at the same lexical nesting level
// they are implemented with '{' '}'
// pthread_cleanup_pop with non-zero argument/pthread_cancel/pthread_exit
// will cause the remained handler to be popped and executed
#define ERROR printf("error @ %s line %d\n", __FILE__, __LINE__ );
void thread_cleanup( void *arg )
{
// __func__ can be used instead
printf("%s is called\n", __FUNCTION__ );
}
void* threadfn( void *data )
{
pthread_cleanup_push( thread_cleanup, NULL );
// sleep是一个可以执行线程取消的函数,称为取消点【see man 7 pthreads】;
sleep( 20 );
pthread_cleanup_pop( 1 );
}
int main(int argc, char const *argv[])
{
pthread_t thread;
if ( pthread_create( &thread, NULL, threadfn, NULL) )
{
ERROR;
return -1;
}
sleep( 1 );
// cancel the thread before it exits
if( pthread_cancel( thread ) )
{
ERROR;
return -1;
}
// wait for thread to complete it's cleanup
if( pthread_join( thread, NULL ) )
{
ERROR;
return -1;
}
printf("Done!\n");
return 0;
}
- pthread——pthread_cleanup
- 多线程 ——pthread
- 线程学习——Pthread
- 线程——create pthread
- Linux Pthread——1
- Linux Pthread——2
- Linux Pthread——3
- pthread(一) —— pthread_create & pthread_join
- 跨平台线程库——pThread
- Pthread
- pthread
- pthread
- PThread
- pthread
- pthread
- pthread
- pthread
- pthread
- python学习笔记(3)
- 手把手让你爱上Android sdk自带“9妹”(9patch 工具)
- 算法学习之排序算法:快速排序
- uva307Sticks
- Freemarker list 对象取前几条数据
- pthread——pthread_cleanup
- 每周精选编程技术文章与资源_9月1日至9月7日
- PowerDesigner显示或隐藏字段
- 三种Div高度自适应的方法_前端开发教程
- Android游戏开发之Cocos2d-android-1 安装运行 (一)
- 13 View工作原理【消息类型与按键消息派发】
- hdu1530 Maximum Clique,最大团 , DP,邻接矩阵
- 链接服务器'(null)'的OLE DB访问接口'STREAM'返回了对列 '[!BulkInsert]
- linux 静态库、共享库