目录
pthread_create
在调用这个函数的进程中创建函数原型
#include <pthread.h>
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg);
参数
thread
属于结果参数。函数结束时,返回线程ID并存储到thread中。
pthread_t
被定义成unsigned long int
类型。
attr
设置线程的属性,主要是栈相关的属性。
start_routine
start_routine是一个回调函数(函数指针实现)。指明了线程要执行的函数。
arg
回调函数start_routine()执行时的参数(实参)。
返回值
成返回0,失败返回非0值。一个新的线程
pthread_exit
结束一个线程
函数原型
#include <pthread.h>
void pthread_exit(void*retval);
参数
retval用来保存线程退出状态
返回值
为空。因为该函数永远成功。
pthread_join
为了回收资源,主线程会等待子线程结束。该函数就是用来等待线程终止的。类似与进程中的wait函数。
此函数将阻塞调用当前线程的进程,直到此线程退出。
函数原型
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
参数
thread
被等待线程的ID
retval
如果此值非NULL,pthread_join复制线程的退出状态
返回值
成功返回0,失败返回非0