VLink  2.0.0
A high-performance communication middleware
condition_variable.h 文件参考

Monotonic-clock condition variable replacement immune to system clock jumps. 更多...

#include <condition_variable>
condition_variable.h 的引用(Include)关系图:
此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

命名空间

 

类型定义

using vlink::ConditionVariable = std::condition_variable
 
using vlink::ConditionVariableAny = std::condition_variable_any
 
using vlink::condition_variable = ConditionVariable
 
using vlink::condition_variable_any = ConditionVariableAny
 

详细描述

Monotonic-clock condition variable replacement immune to system clock jumps.

Older libstdc++ implementations route every std::condition_variable timed wait through CLOCK_REALTIME, so an NTP step or manual date change can spuriously wake or starve waiters (GCC PR 41861 / DR 887). On POSIX systems this header substitutes a hand-rolled vlink::ConditionVariable backed by a pthread_cond_t configured with pthread_condattr_setclock (..., CLOCK_MONOTONIC). On Windows the bug does not apply and the names alias to the standard library types verbatim.

API surface vs std::condition_variable
Aspect std::condition_variable vlink::ConditionVariable
Backing clock CLOCK_REALTIME (libstdc++) CLOCK_MONOTONIC via pthread_condattr
Timed waits Sensitive to wall clock jumps Immune to wall clock jumps
Copy / move Deleted Deleted
Public methods wait / wait_for / wait_until / notify_* Same signatures, same return types
Native handle pthread_cond_t* pthread_cond_t*
Wait / notify sequence
*   producer thread                consumer thread
*   ---------------                 ---------------
*   lock(mtx)                       lock(mtx)
*   state = ready                   cv.wait(lock, predicate)
*   unlock(mtx)                       releases mtx, blocks
*   cv.notify_one()  ----------->     wakes; reacquires mtx
*                                     re-checks predicate
*                                     returns to caller
* 

Both ConditionVariable and ConditionVariableAny accept any std::chrono clock type; non-steady clock arguments are projected onto steady_clock at entry and re-checked at exit for timeout fidelity. Convenience type aliases vlink::condition_variable and vlink::condition_variable_any are also exposed.

Example
std::mutex mtx;
bool ready = false;
// Consumer:
{
std::unique_lock lock(mtx);
cv.wait_for(lock, std::chrono::milliseconds(200), [&] { return ready; });
}
// Producer:
{
std::lock_guard lock(mtx);
ready = true;
}
cv.notify_one();