DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ConditionVariable.cpp
Go to the documentation of this file.
1#include "pch.h"
4#include <condition_variable>
5#include <mutex>
6#include <chrono>
7
8namespace DotNetDupe {
9 namespace System {
10 namespace Threading {
11
12 struct CSLockAdapter {
13 CriticalSection& cs;
14 void lock() { cs.Enter(); }
15 void unlock() { cs.Leave(); }
16 };
17
18 struct ConditionVariable::Impl : public Object {
19 std::condition_variable_any cv;
20 };
21
25
27
29
32
35 CSLockAdapter adapter{cs};
36 m_pImpl->cv.wait(adapter);
37 }
38
39 bool ConditionVariable::Wait(CriticalSection& cs, int millisecondsTimeout) {
41 if (millisecondsTimeout < 0) {
42 Wait(cs);
43 return true;
44 }
45
47 CSLockAdapter adapter{cs};
48 auto status = m_pImpl->cv.wait_for(adapter, std::chrono::milliseconds(millisecondsTimeout));
49 return status == std::cv_status::no_timeout;
50 }
51
54 m_pImpl->cv.notify_one();
55 }
56
59 m_pImpl->cv.notify_all();
60 }
61 }
62 }
63}
Provides condition variable synchronization primitive for thread coordination.
Provides a re-entrant mutual exclusion primitive for thread synchronization.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Provides a synchronization primitive that can be used to block a thread, or multiple threads,...
void Wait(CriticalSection &cs)
Releases the associated CriticalSection lock and blocks the current thread until signaled.
ConditionVariable()
Initializes a new instance of the ConditionVariable class.
bool Wait(CriticalSection &cs, int millisecondsTimeout)
Releases the associated CriticalSection lock and blocks the current thread until signaled or timeout ...
void PulseAll()
Notifies all waiting threads.
ConditionVariable & operator=(const ConditionVariable &)=delete
virtual ~ConditionVariable()
Virtual destructor releasing condition variable resources.
Provides a re-entrant mutual exclusion primitive for thread synchronization.