zoukankan      html  css  js  c++  java
  • 条件变量和信号量的区别

    首先第一个区别条件变量有广播的功能,所以当实现订阅的时候,需要广播事件的时候必须使用条件变量,而semaphore只能出发一个订阅

    有人这么说:

    Conditional variable is essentially a wait-queue, that supports blocking-wait and wakeup operations, i.e. you can put a thread into the wait-queue and set its state to BLOCK, and get a thread out from it and set its state to READY.

    Note that to use a conditional variable, two other elements are needed:

    • a condition (typically implemented by checking a flag or a counter)
    • a mutex that protects the condition

    The protocol then becomes,

    1. acquire mutex
    2. check condition
    3. block and release mutex if condition is true, else release mutex

    Semaphore is essentially a counter + a mutex + a wait queue. And it can be used as it is without external dependencies. You can use it either as a mutex or as a conditional variable.

    Therefore, semaphore can be treated as a more sophisticated structure than conditional variable, while the latter is more lightweight and flexible.

    是我目前看到比较清晰的解释。

    另外有些场景使用semphore没有条件变量好,比如:

    semaphore and condition variables are very similar and are used mostly for the same purposes. However, there are minor differences that could make one preferable. For example, to implement barrier synchronization you would not be able to use a semaphore.But a condition variable is ideal.

    Barrier synchronization is when you want all of your threads to wait until everyone has arrived at a certain part in the thread function. this can be implemented by having a static variable which is initially the value of total threads decremented by each thread when it reaches that barrier. this would mean we want each thread to sleep until the last one arrives.A semaphore would do the exact opposite! with a semaphore, each thread would keep running and the last thread (which will set semaphore value to 0) will go to sleep.

    a condition variable on the other hand, is ideal. when each thread gets to the barrier we check if our static counter is zero. if not, we set the thread to sleep with the condition variable wait function. when the last thread arrives at the barrier, the counter value will be decremented to zero and this last thread will call the condition variable signal function which will wake up all the other threads!

  • 相关阅读:
    由1433端口入侵,浅谈sqlserver安全 (转)
    使用 Aircrack-ng 破解 WEP 和 WPA/WPA2 加密的 Wi-Fi 密码。(转)
    ZZmsvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义 .的分析解决办法 (转)
    提高D3js力导向图加载速度(转)
    Asp.Net实现FORM认证的一些使用技巧(转)
    Windows Server 2008 R2 备份和恢复 (转)
    搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
    Beego源码分析(转)
    go语言实现一个简单的登录注册web小程序
    从无线安全到内网渗透(转)
  • 原文地址:https://www.cnblogs.com/biglucky/p/6347863.html
Copyright © 2011-2022 走看看