linux线程相关函数

来源:互联网 发布:网络银商最重的判刑 编辑:程序博客网 时间:2024/04/30 11:53

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

例子下载:
pthread_clean函数:thread_clean.c
pthread_create函数:thread_creat.c
线程发送消息队列:thread_msgrcv.c
线程接收消息队列:thread_msgsnd.c

1.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

 

2.void pthread_exit(void *ral_ptr)

终止线程

头文件:#include <pthread.h>

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

 

3.int pthread_join(pthread_t tid,void *rval_ptr)

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

头文件:#include <pthread.h>

tid:等待退出的线程id

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

 

4.pthread_t pthread_self(void)

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

头文件:#include <pthread.h>

 

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

将清除函数压入清除栈

头文件:#include <pthread.h>

rtn:清除函数指针

arg:清除函数的参数

 

6.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()中所指定的清除函数

 

例子下载:
pthread_clean函数:thread_clean.c
pthread_create函数:thread_creat.c
线程发送消息队列:thread_msgrcv.c
线程接收消息队列:thread_msgsnd.c

原创粉丝点击