一、概述
本节将继续说明有关线程编程常用 API 的使用方法,主要说一下与线程条件变量及线程信号通知的 API。通过这些 API 可以实现线程之间的同步及通信机制。
二、线程条件变量 API
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 库中的半驻留式线程池就用到了它。
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);
三、示例
#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。
acl 库下载:https://sourceforge.net/projects/acl/
github: https://github.com/acl-dev/acl
本文深入介绍了线程编程中线程条件变量与信号通知的API,包括初始化/销毁线程条件变量、等待线程条件变量被通知、通知阻塞线程等关键操作。并通过示例代码展示了如何在实际应用中灵活运用这些API来实现线程间的同步与通信。
105

被折叠的 条评论
为什么被折叠?



