#include <pthread.h> int pthread_equal(pthread_t tid1, pthread_t tid2); //如果相等返回非0值,否则返回0.线程可以通过调用pthread_self函数获得自身线程ID。
#include<pthread.h> pthread_t pthread_self(void); //返回值为调用线程的线程ID。
#include<pthread.h> int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void*), void*restrict arg ); //若成功则返回0,否则返回错误编号当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID,attr参数用于定制各种不同的线程
#include<pthread.h> void pthread_exit(void *rval_ptr);rval_ptr是一个无类型指针,与传给启动例程的单个参数类型,进程中的其他线程可以通过调用pthread_join函数访问
#include <pthread.h> int pthread_join(pthread_t thread, void **rval_ptr); //若成功则返回0,否则返回错误编号。调用线程将一直阻塞,知道指定的线程调用pthread_exit、从启动例程中返回或者取消。如果线程只是从它的启动例程
#include <stdio.h> #include <pthread.h> #include <string.h> void* thr_fn1(void *arg){ printf("thread 1 return.\n"); return (void*)1; } void* thr_fn2(void *arg){ printf("thread 2 exit.\n"); pthread_exit((void*)2); } int main(void){ int err; pthread_t tid1,tid2; void* tret; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if(err != 0){ printf("pthread_create:%s\n",strerror(err)); return -1; } err = pthread_create(&tid2, NULL, thr_fn2, NULL); if(err != 0){ printf("pthread_create:%s\n",strerror(err)); return -1; } err = pthread_join(tid1, &tret); if(err != 0){ printf("pthread_join:%s\n",strerror(err)); return -1; } printf("thread1 exit code %d\n",(int)tret); err = pthread_join(tid2, &tret); if(err != 0){ printf("pthread_join:%s\n",strerror(err)); return -1; } printf("thread1 exi2 code %d\n",(int)tret); return 0; }
#include<pthread.h> int pthread_cancel(pthread_t itd); //若成功则返回0,否则返回错误编号
#include<pthread.h> void pthread_cleanup_push(void (*rtn)(void *), void* arg); void pthread_cleanup_pop(int execute);当线程执行以下动作时调用清理函数,调用参数为arg,清理函数rtn的调用顺序是由pthread_clean_push函数来安排的。
#include <stdio.h> #include <pthread.h> void cleanup(void *arg){ printf("cleanup:%s\n",(char*)arg); } void *thr_fn1(void *arg){ printf("thread 1 start\n"); pthread_cleanup_push(cleanup, "thread 1 first handler"); pthread_cleanup_push(cleanup, "thread 1 second handler"); printf("thread 1 push complete\n"); return (void *)1; pthread_cleanup_pop(0); pthread_cleanup_pop(0); } void *thr_fn2(void *arg){ printf("thread 2 start\n"); pthread_cleanup_push(cleanup, "thread 2 first handler"); pthread_cleanup_push(cleanup, "thread 2 second handler"); printf("thread 2 push complete\n"); pthread_exit((void *)2); pthread_cleanup_pop(0); pthread_cleanup_pop(0); } int main(void){ pthread_t tid1,tid2; int err; void* tret; err = pthread_create(&tid1, NULL, thr_fn1, (void*)1); if(err != 0){ printf("pthread_create:%s\n",strerror(err)); return -1; } err = pthread_create(&tid2, NULL, thr_fn2, (void*)2); if(err != 0){ printf("pthread_create:%s\n",strerror(err)); return -1; } err = pthread_join(tid1,&tret); if(err != 0){ printf("pthread_join:%s\n",strerror(err)); return -1; } printf("thread 1 exit code:%d\n",(int)tret); err = pthread_join(tid2,&tret); if(err != 0){ printf("pthread_join:%s\n",strerror(err)); return -1; } printf("thread 2 exit code:%d\n",(int)tret); return 0; }
#include <pthread.h> int pthread_detach(pthread_t tid); //成功则返回1,否则返回错误编号。
#include <stdio.h> #include <pthread.h> #include <string.h> void *thr_fn(void *arg){ printf("thread start\n"); printf("thread complete\n"); pthread_exit((void *)1); } int main(void){ pthread_t tid1; int err; void* tret; err = pthread_create(&tid1, NULL, thr_fn, (void*)1); if(err != 0){ printf("pthread_create:%s\n",err); return -1; } err = pthread_detach(tid1); if(err != 0){ printf("pthread_detach:%s\n",err); return -1; } err = pthread_join(tid1,&tret); if(err != 0){ printf("pthread_join:%s\n",strerror(err)); return -1; } printf("thread 1 exit code:%d\n",(int)tret); return 0; }