pthread_create

int pthread_create(pthread_t* tid, const pthread_atrr, void*(*func)(void*), void* arg);
func:接受void*,返回void*,arg是唯一的参数
pthread_join:等待一个给定线程终止。当一个joinable的线程终止时,他的线程id和退出状态留存到另一个线程对他调用pthread_join.
pthread_self:获得自身线程id
pthread_detch:使线程脱离,更像是守护进程,线程终止时,所有的相关资源都被释放,不能等待他们终止
pthred_exit:线程终止,其他两种线程终止(线程函数放回,进程调用exit)

互斥锁:pthread_mutex

int pthread_mutex_lock(pthread_mutex_t* mptr);
int pthread_mutex_unlock(pthread_mutex_t* mptr);

我们需要一个让主循环进入睡眠,直到某个线程通知他有事可做才醒过来。条件变量结合互斥锁能够提供这个功能。
互斥锁提供互斥机制,条件变量提供信号机制

int pthread_cond_wait(pthread_cond_t* cptr, pthread_mutex_t* mptr);//把调用线程投入睡眠并释放调用线程持有的互斥锁。当收到信号唤醒时,线程重新获得该锁
int pthread_cond_signal(pthread_cond_t* cptr);//唤醒等在相应条件变量上的单个线程
int pthread_cond_broadcast(pthread)cond_t* cptr);//唤醒等在相应条件变量上的所有线程
int pthread_cond_timewait(pthread_cond_t* cptr, pthread_mutex_t* mptr, const struct timespec* abstime);//等待条件变量设置一个超时时间

posted @ 2019-12-29 21:28  zzyoucan  阅读(719)  评论(0编辑  收藏  举报