V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
nthhdy
V2EX  ›  Linux

请教关于 pthread condition 的使用场景

  •  
  •   nthhdy ·
    workingenius · 2019-09-11 11:22:16 +08:00 · 3325 次点击
    这是一个创建于 1660 天前的主题,其中的信息可能已经有所发展或是发生改变。

    根据 stackoverflow 上这篇贴,condition 的用法可以总结为

    // safely examine the condition, prevent other threads from
    // altering it
    pthread_mutex_lock (&lock);
    while ( SOME-CONDITION is false)
        pthread_cond_wait (&cond, &lock);
    
    // Do whatever you need to do when condition becomes true
    do_stuff();
    pthread_mutex_unlock (&lock);
    

    请问为什么不直接写成这样呢?

    while (true) {
        pthread_mutex_lock (&lock);
        if ( SOME-CONDITION is true) {
            do_stuff();
            pthread_mutex_unlock (&lock);
            break;
        } else {
            pthread_mutex_unlock (&lock);
        }
    }
    

    这样,不需要 condition 这个概念,也可以把临界区保护起来

    5 条回复    2019-09-12 10:34:23 +08:00
    ysc3839
        1
    ysc3839  
       2019-09-11 11:34:47 +08:00 via Android
    后者不停地 unlock 和 lock,开销太大了吧?等于是轮询了。
    pthread_cond_wait 等待时应该是不占用 CPU 时间的。
    ipwx
        2
    ipwx  
       2019-09-11 11:39:54 +08:00 via Android
    cond wait 有系统维护等待队列。之所以这里用 while 是为了防止多重唤醒吧
    hu8245
        3
    hu8245  
       2019-09-11 11:42:00 +08:00 via Android
    pthread_cond_wait() 用于阻塞当前线程,等待别的线程使用 pthread_cond_signal()或 pthread_cond_broadcast 来唤醒它。 pthread_cond_wait() 必须与 pthread_mutex
    配套使用。pthread_cond_wait()函数一进入 wait 状态就会自动 release mutex。当其他线程通过 pthread_cond_signal()或 pthread_cond_broadcast,把该线程唤醒,使 pthread_cond_wait()通过(返回)时,该线程又自动获得该 mutex。


    ---------
    也就是说,检测到条件变量不满足,就释放锁,睡眠。等到相关信号唤醒自己,重新加锁,继续执行。一楼说的对,while (true)的 cpu 的开销太高了
    husinhu
        4
    husinhu  
       2019-09-11 11:50:55 +08:00
    是的你这样就是轮询了,一直在循环并且拿 /释放锁。
    第一个 while 目的是保证两个线程的执行顺序。后面就不会轮询了。
    nthhdy
        5
    nthhdy  
    OP
       2019-09-12 10:34:23 +08:00
    感谢各位 @ysc3839 @ipwx @hu8245 @husinhu

    明白了,问题就在于,我这么写是忙等,平白无故耗 cpu
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3213 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:06 · PVG 21:06 · LAX 06:06 · JFK 09:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.