Moderators: Sascha Willems, walaber
class dgSemaphore
{
public:
dgSemaphore ();
void Wait();
void Release();
private:
std::condition_variable m_cond;
std::mutex m_mutex;
unsigned m_count;
};
dgThread::dgSemaphore::dgSemaphore ()
{
m_count = 0;
}
void dgThread::dgSemaphore::Wait()
{
std::unique_lock<std::mutex> lock( m_mutex );
m_cond.wait( lock, [this](){ return m_count > 0; } );
--m_count;
}
void dgThread::dgSemaphore::Release ()
{
std::unique_lock<std::mutex> lock( m_mutex );
++m_count;
m_cond.notify_one( );
}
aitzolmuelas wrote: but from what I have read around most current mutex implementations use an atomic to avoid the system call if the mutex is not owned by any thread.
Users browsing this forum: No registered users and 0 guests