
Roland Schwarz wrote:
Yuval Ronen wrote:
I will gladly put my code here to be scrutinized by the experts on this forum (after the weekend, as the code is at work, and I'm at home now :-) ), but I fear that they will little incentive to do it.
Can't tell. Possibly yes. You will need to try.
Ok, if anyone is still interested, here's my implementation of CV for Windows. It's very simple, maybe too simple, and I guess it's far less interesting than the code just posted by Chris Thomasson, but here it is anyway. #ifndef WinCountingSem_h #define WinCountingSem_h #include <Windows.h> #include <cassert> namespace PlatformSpecific { class WinCountingSem { public: typedef HANDLE SemphoreImpl; static const size_t maxValue = 0xFFFFFFL; public: WinCountingSem(size_t a_initialValue, size_t a_maxValue = maxValue) { m_impl = ::CreateSemaphore(NULL, a_initialValue, a_maxValue, NULL); assert(m_impl != NULL); } ~WinCountingSem() { BOOL res = ::CloseHandle(m_impl); assert(res == true); } void lock() { DWORD res = ::WaitForSingleObject(m_impl, INFINITE); assert(res == WAIT_OBJECT_0); } void unlock(size_t a_count = 1) { BOOL res = ::ReleaseSemaphore(m_impl, a_count, NULL); assert(res == true); } private: SemphoreImpl m_impl; WinCountingSem(const WinCountingSem &); WinCountingSem & operator=(const WinCountingSem &); }; } // namespace PlatformSpecific #endif /* ! defined WinCountingSem_h */ #ifndef WinConditionVar_h #define WinConditionVar_h #include <cassert> #include "Mutex.h" #include "WinCountingSem.h" namespace PlatformSpecific { class WinConditionVar { public: WinConditionVar(Mutex &a_mutex) : m_waitersCount(0), m_sem(0), m_mutex(a_mutex) { } void wait() { assert(m_mutex.isLocked()); m_waitersCount++; m_mutex.unlock(); m_sem.lock(); m_mutex.lock(); } void signal() { assert(m_mutex.isLocked()); if (m_waitersCount > 0) { m_sem.unlock(); m_waitersCount--; } } void broadcast() { assert(m_mutex.isLocked()); if (m_waitersCount > 0) { m_sem.unlock(m_waitersCount); m_waitersCount = 0; } } private: size_t m_waitersCount; WinCountingSem m_sem; Mutex &m_mutex; WinConditionVar(const WinConditionVar &); WinConditionVar & operator=(const WinConditionVar &); }; } // namespace PlatformSpecific #endif /* ! defined WinConditionVar_h */