linux线程相关函数

 时间:2026-02-17 10:35:30

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)

  • 什么是线程?
  • 线程实现的三种方法
  • 线程之间如何通信
  • 线程如何进入运行状态
  • 线程间同步的方法
  • 热门搜索
    c反应蛋白偏高说明什么 et是什么 自负是什么意思 慢性宫颈炎用什么药 酒酿是什么 结婚纪念日送什么礼物 鼻烟是什么 父亲节是什么时候 齿轮模数是什么意思 栩栩如生什么意思