多线程编程API简介 下

来源:互联网 发布:右友是一款什么软件 编辑:程序博客网 时间:2024/05/19 07:08

一、概述

      本节将继续说明有关线程编程常用 API 的使用方法,主要说一下与线程条件变量及线程信号通知的 API。通过这些 API 可以实现线程之间的同步及通信机制。

 

二、线程条件变量 API

 1)初始化/销毁线程条件变量:pthread_cond_init/pthread_cond_destroy;在 acl 库中相应的 API 为 acl_pthread_cond_init/acl_pthread_cond_destroy。

C代码  收藏代码
  1. /** 
  2.  * 用线程条件变量属性初始化线程条件变量对象 
  3.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  4.  * @param attr {const pthread_condattr_t*} 条件变量属性,用来设置线程 
  5.  *    条件变量的属性,该参数可以由 pthread_condattr_init/pthread_condattr_destroy 初始化和销毁;该参数可以设为 NULL 
  6.  * @return {int} 返回 0 表示成功,否则出错,错误号可以用 strerror 打印 
  7.  */  
  8. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);  
  9.   
  10. /** 
  11.  * 销毁线程条件变量对象通过 pthread_cond_init 分配的资源 
  12.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  13.  * @return {int} 返回 0 表示成功,否则出错 
  14.  */  
  15. 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。

C代码  收藏代码
  1. /** 
  2.  * 线程阻塞在线程条件变量直至该线程条件变量被通知,在等待状态,该线程不会 
  3.  * 拥有线程锁;当线程条件变量被通知时,该线程首先会对线程锁加锁,然后返回 
  4.  * 给调用者,即当该 API 返回时,当前线程已经拥有了其中的线程锁 
  5.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  6.  * @param mutex {pthread_mutex_t*} 与线程条件变量配合使用的线程锁 
  7.  * @return {int} 返回 0 表示阻塞当前线程的线程条件变量被其它线程通知,同 
  8.  *   时当前线程获得相应的线程锁;否则,表示出错,出错原因一般是输入的参数非法 
  9.  */  
  10. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t  *mutex);  
  11.   
  12. /** 
  13.  * 当前线程阻塞在线程条件变量上,直到该条件变量被其它线程通知或设定的等待 
  14.  * 超时时间到达,该 API 相比 pthread_cond_wait 多出一个等待超时时间 
  15.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  16.  * @param mutex {pthread_mutex_t*} 与线程条件变量配合使用的线程锁 
  17.  * @param abstime {const struct timespec*} 超时时间截,当时间超过该 
  18.  *    时间截后,即使线程条件变量未被通知,该 API 也会返回 
  19.  * @return {int} 返回 0 表示阻塞当前线程的线程条件变量被其它线程通知,同 
  20.  *   时当前线程获得相应的线程锁;否则,如果返回值为 ETIMEDOUT(在 acl 库 
  21.  *   中表示为 ACL_ETIMEDOUT)则表示该 API 是因为超时才返回的,同时会将 
  22.  *   线程锁加锁,当为其它返回值时一般是因为输入参数非法导致 
  23.  */  
  24. 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。

C代码  收藏代码
  1. /** 
  2.  * 唤醒阻塞在某个线程条件变量上的一个线程 
  3.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  4.  * @return {int} 返回 0 表示成功,否则表示出错 
  5.  */  
  6. int pthread_cond_signal(pthread_cond_t *cond);  
  7.   
  8. /** 
  9.  * 唤醒阻塞在某个线程条件变量上的所有线程,当所有阻塞在线程条件变量上的 
  10.  * 所有线程被唤醒后,会首先抢占参数中输入的线程锁(有可能是同一个线程锁, 
  11.  * 也有可能不是),在获得那个线程锁后那些被唤醒的线程才会返回 
  12.  * @param cond {pthread_cond_t*} 线程条件变量对象 
  13.  * @return {int} 返回 0 表示成功通知所有阻塞在线程条件变量上的线程,否则 
  14.  *    表示出错 
  15.  */  
  16. int pthread_cond_broadcast(pthread_cond_t *cond);  

 

三、示例

C代码  收藏代码
  1. #include <stdio.h>  
  2. #include <assert.h>  
  3. #include <pthread.h>  
  4.   
  5. /* 快速初始化线程锁和线程条件变量 */  
  6. static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER;  
  7. static pthread_cond_t __cond = PTHREAD_COND_INITIALIZER;  
  8.   
  9. static void *thread_waiting(void* arg)  
  10. {  
  11.     (void) arg;  
  12.     printf("thread: %ld waiting ...\r\n", pthread_self());  
  13.   
  14.     /* 阻塞在线程条件变量上, */  
  15.     assert(pthread_cond_wait(__cond, __mutex) == 0);  
  16.   
  17.     /* 该线程被唤醒,同时拥有了线程锁 __mutex */  
  18.     printf("thread: %ld wakeup by other thread\r\n", pthread_self());  
  19.     return NULL;  
  20. }  
  21.   
  22. int main(void)  
  23. {  
  24.     pthread_t tid;  
  25.   
  26.     /* 创建阻塞在线程条件变量上的子线程 */  
  27.     assert(pthread_create(&tid, NULL, thread_waiting, NULL) == 0);  
  28.     sleep(10);  /* 主线程休息 10 秒 */  
  29.   
  30.     /* 唤醒阻塞在线程条件变量上的子线程 */  
  31.     assert(pthread_cond_signal(__cond) == 0);  
  32.   
  33.     /* 接管子线程的退出状态 */  
  34.     assert(pthread_join(&tid) == 0);  
  35.   
  36.     return 0;  
  37. }  

       该例子非常简单,用户可以在自己的程序中灵活使用这些 API。

0 0
原创粉丝点击