1、概述编程
本节将继续说明有关线程编程经常使用 API 的使用方法,主要说一下与线程条件变量及线程信号通知的 API。经过这些 API 能够实现线程之间的同步及通讯机制。spa
2、线程条件变量 API.net
1)初始化/销毁线程条件变量:pthread_cond_init/pthread_cond_destroy;在 acl 库中相应的 API 为 acl_pthread_cond_init/acl_pthread_cond_destroy。线程
/** * 用线程条件变量属性初始化线程条件变量对象 * @param cond {pthread_cond_t*} 线程条件变量对象 * @param attr {const pthread_condattr_t*} 条件变量属性,用来设置线程 * 条件变量的属性,该参数能够由 pthread_condattr_init/pthread_condattr_destroy 初始化和销毁;该参数能够设为 NULL * @return {int} 返回 0 表示成功,不然出错,错误号能够用 strerror 打印 */ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); /** * 销毁线程条件变量对象经过 pthread_cond_init 分配的资源 * @param cond {pthread_cond_t*} 线程条件变量对象 * @return {int} 返回 0 表示成功,不然出错 */ int pthread_cond_destroy(pthread_cond_t *cond);
2)等待线程条件变量被其它线程通知:pthread_cond_wait/pthread_cond_timedwait,在 acl 库中对应的 API 为 acl_pthread_cond_wait/acl_pthread_cond_timedwait。设计
/** * 线程阻塞在线程条件变量直至该线程条件变量被通知,在等待状态,该线程不会 * 拥有线程锁;当线程条件变量被通知时,该线程首先会对线程锁加锁,而后返回 * 给调用者,即当该 API 返回时,当前线程已经拥有了其中的线程锁 * @param cond {pthread_cond_t*} 线程条件变量对象 * @param mutex {pthread_mutex_t*} 与线程条件变量配合使用的线程锁 * @return {int} 返回 0 表示阻塞当前线程的线程条件变量被其它线程通知,同 * 时当前线程得到相应的线程锁;不然,表示出错,出错缘由通常是输入的参数非法 */ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); /** * 当前线程阻塞在线程条件变量上,直到该条件变量被其它线程通知或设定的等待 * 超时时间到达,该 API 相比 pthread_cond_wait 多出一个等待超时时间 * @param cond {pthread_cond_t*} 线程条件变量对象 * @param mutex {pthread_mutex_t*} 与线程条件变量配合使用的线程锁 * @param abstime {const struct timespec*} 超时时间截,当时间超过该 * 时间截后,即便线程条件变量未被通知,该 API 也会返回 * @return {int} 返回 0 表示阻塞当前线程的线程条件变量被其它线程通知,同 * 时当前线程得到相应的线程锁;不然,若是返回值为 ETIMEDOUT(在 acl 库 * 中表示为 ACL_ETIMEDOUT)则表示该 API 是由于超时才返回的,同时会将 * 线程锁加锁,当为其它返回值时通常是由于输入参数非法致使 */ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
上面两个等待线程条件变量的 API中,pthread_cond_timedwait 对于设计半驻留式线程很是有用,象 acl 库中的半驻留式线程池就用到了它。code
3)通知阻塞在线程条件变量上的线程:pthread_cond_signal/pthread_cond_broadcast,在 acl 库中相应表现形式为:acl_pthread_cond_signal/acl_pthread_cond_broadcast。对象
/** * 唤醒阻塞在某个线程条件变量上的一个线程 * @param cond {pthread_cond_t*} 线程条件变量对象 * @return {int} 返回 0 表示成功,不然表示出错 */ int pthread_cond_signal(pthread_cond_t *cond); /** * 唤醒阻塞在某个线程条件变量上的全部线程,当全部阻塞在线程条件变量上的 * 全部线程被唤醒后,会首先抢占参数中输入的线程锁(有多是同一个线程锁, * 也有可能不是),在得到那个线程锁后那些被唤醒的线程才会返回 * @param cond {pthread_cond_t*} 线程条件变量对象 * @return {int} 返回 0 表示成功通知全部阻塞在线程条件变量上的线程,不然 * 表示出错 */ int pthread_cond_broadcast(pthread_cond_t *cond);
3、示例blog
#include <stdio.h> #include <assert.h> #include <pthread.h> /* 快速初始化线程锁和线程条件变量 */ static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t __cond = PTHREAD_COND_INITIALIZER; static void *thread_waiting(void* arg) { (void) arg; printf("thread: %ld waiting ...\r\n", pthread_self()); /* 阻塞在线程条件变量上, */ assert(pthread_cond_wait(__cond, __mutex) == 0); /* 该线程被唤醒,同时拥有了线程锁 __mutex */ printf("thread: %ld wakeup by other thread\r\n", pthread_self()); return NULL; } int main(void) { pthread_t tid; /* 建立阻塞在线程条件变量上的子线程 */ assert(pthread_create(&tid, NULL, thread_waiting, NULL) == 0); sleep(10); /* 主线程休息 10 秒 */ /* 唤醒阻塞在线程条件变量上的子线程 */ assert(pthread_cond_signal(__cond) == 0); /* 接管子线程的退出状态 */ assert(pthread_join(&tid) == 0); return 0; }
该例子很是简单,用户能够在本身的程序中灵活使用这些 API。资源
我的微博:http://weibo.com/zsxxszget