linux线程相关函数

2025-10-31 07:16:07

int pthread_create(pthread_t *tidp,const pthread_addr_t *attr,void *(start_rtn)(void),void *arg)

创建线程

头文件:#include <pthread.h>

tidp:线程id

attr:线程属性(通常为空)

start_rtn:线程要执行的函数(函数必须是void *型的返回值)

arg:start_rtn的参数

返回值:创建成功时返回0

void pthread_exit(void *ral_ptr)

终止线程

头文件:#include <pthread.h>

rval_ptr:线程退出时,返回值的指针

int pthread_join(pthread_t tid,void *rval_ptr)

阻塞父进程,直到指定的线程终止

头文件:#include <pthread.h>

tid:等待退出的线程id

rval_ptr:线程退出时返回值的指针

pthread_t pthread_self(void)

获取线程的标示符(线程id)

头文件:#include <pthread.h>

void pthread_cleanup_push(void (*rtn)(void *),void *arg)

将清除函数压入清除栈

头文件:#include <pthread.h>

rtn:清除函数指针

arg:清除函数的参数

void pthread_cleanup_pop(int execute)

将清除函数弹出清除栈

头文件:#include <pthread.h>

execute:当执行到pthread_cleanup_pop()时是否在弹出清除函数的同时执行该清除函数,0:不执行,非0:执行

** 从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的程序段中的终止动作(包括pthread_exit()和异常终止,不包括return)都将执行pthread_cleanup_push()中所指定的清除函数

ps:

linux中需要使用库libpthread.a,所以编译时需要加上-lpthread(#gcc filename -lpthread)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:154
  • 阅读量:176
  • 阅读量:73
  • 阅读量:23
  • 阅读量:36
  • 猜你喜欢