免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2083 | 回复: 5
打印 上一主题 下一主题

请教 pthread_cond_wait [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-26 16:11 |只看该作者 |倒序浏览
本帖最后由 juruqiang 于 2011-12-26 16:11 编辑

APUE2中文讲pthread_cond_wait说到,

“函数把调用线程放到等待条件的线程列表上,然后对互斥变量解锁,这两个操作是原子操作。 ---(1)
  这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道......”   ---(2)

我查看源码,大概过程是:
条件检查->enqueue->解锁->suspend(阻塞引起休眠)......      (理解对否???)

问题:
1、pthread_cond_wait中的处理顺序?
2、(1)(2)怎么理解?



附源码:
int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
  volatile pthread_descr self = thread_self();
  pthread_extricate_if extr;
  int already_canceled = 0;
  int spurious_wakeup_count;

  /* Check whether the mutex is locked and owned by this thread.  */
  if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
      && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
      && mutex->__m_owner != self)
    return EINVAL;

  /* Set up extrication interface */
  extr.pu_object = cond;
  extr.pu_extricate_func = cond_extricate_func;

  /* Register extrication interface */
  THREAD_SETMEM(self, p_condvar_avail, 0);
  __pthread_set_own_extricate_if(self, &extr);

  /* Atomically enqueue thread for waiting, but only if it is not
     canceled. If the thread is canceled, then it will fall through the
     suspend call below, and then call pthread_exit without
     having to worry about whether it is still on the condition variable queue.
     This depends on pthread_cancel setting p_canceled before calling the
     extricate function. */

  __pthread_lock(&cond->__c_lock, self);
  if (!(THREAD_GETMEM(self, p_canceled)
      && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))  //条件检查
    enqueue(&cond->__c_waiting, self);
  else
    already_canceled = 1;
  __pthread_unlock(&cond->__c_lock);

  if (already_canceled) {
    __pthread_set_own_extricate_if(self, 0);
    __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  }

  pthread_mutex_unlock(mutex);

  spurious_wakeup_count = 0;
  while (1)
    {
      suspend(self);
      if (THREAD_GETMEM(self, p_condvar_avail) == 0
          && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
              || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
        {
          /* Count resumes that don't belong to us. */
          spurious_wakeup_count++;
          continue;
        }
      break;
    }

  __pthread_set_own_extricate_if(self, 0);

  /* Check for cancellation again, to provide correct cancellation
     point behavior */

  if (THREAD_GETMEM(self, p_woken_by_cancel)
      && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
    THREAD_SETMEM(self, p_woken_by_cancel, 0);
    pthread_mutex_lock(mutex);
    __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  }

  /* Put back any resumes we caught that don't belong to us. */
  while (spurious_wakeup_count--)
    restart(self);

  pthread_mutex_lock(mutex);
  return 0;
}

论坛徽章:
0
2 [报告]
发表于 2011-12-27 20:52 |只看该作者
up

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
3 [报告]
发表于 2011-12-28 10:20 |只看该作者
哪里的代码, 我看得代码和你的不一致, 至少 没见到 futex的影子

论坛徽章:
0
4 [报告]
发表于 2011-12-28 20:51 |只看该作者
zylthinking 发表于 2011-12-28 10:20
哪里的代码, 我看得代码和你的不一致, 至少 没见到 futex的影子


glibc-linuxthreads-2.5.tar里的源码

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
5 [报告]
发表于 2011-12-29 10:39 |只看该作者
juruqiang 发表于 2011-12-28 20:51
glibc-linuxthreads-2.5.tar里的源码


似乎最新代码里面已经彻底变了, 最新代码我还看了半天, 能说出点123来, 你的代码实在不想琢磨了

论坛徽章:
0
6 [报告]
发表于 2011-12-29 14:05 |只看该作者
zylthinking 发表于 2011-12-29 10:39
似乎最新代码里面已经彻底变了, 最新代码我还看了半天, 能说出点123来, 你的代码实在不想琢磨了:em0 ...


能说就指教下,按你的代码说也可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP