网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 生活知识 知识问答

linux线程相关函数

时间:2024-11-04 01:56:31

intpthread_create(pthread_t*tidp,constpthread_addr_t*attr,void*(start_rtn)(void),void*arg)

创建线程

头文件:#include<pthread.h>

tidp:线程id

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

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

arg:start_rtn的参数

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

voidpthread_exit(void*ral_ptr)

终止线程

头文件:#include<pthread.h>

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

intpthread_join(pthread_ttid,void*rval_ptr)

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

头文件:#include<pthread.h>

tid:等待退出的线程id

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

pthread_tpthread_self(void)

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

头文件:#include<pthread.h>

voidpthread_cleanup_push(void(*rtn)(void*),void*arg)

将清除函数压入清除栈

头文件:#include<pthread.h>

rtn:清除函数指针

arg:清除函数的参数

voidpthread_cleanup_pop(intexecute)

将清除函数弹出清除栈

头文件:#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(#gccfilename-lpthread)

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com