头文件:#include <pthread.h>
编译:Compile and link with -pthread.
编译指令: gcc thread.c -o thread -lpthread
1、创建线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
2、获取线程id
pthread_t pthread_self(void);
3、线程退出
void pthread_exit(void *retval);
终止自身。
4、线程取消
int pthread_cancel(pthread_t thread);
可从其他线程终止。
5、线程等待
int pthread_join(pthread_t thread, void **retval);
阻塞时等待,可用于在线程结束后手动释放资源。
6、线程分离
int pthread_detach(pthread_t thread);
线程分离后,其资源可在线程结束后自动释放。
7、线程清理函数
void pthread_cleanup_push(void (*routine)(void *),
void *arg);
void pthread_cleanup_pop(int execute);
execute参数表示执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数,为0表示不执行,非0为执行;这个参数并不影响异常终止时清理函数的执行。
参考资料:http://www.xinyingda.cn/bbs/wapbencandy.php?fid=15&id=1081