“事件”对象的一个轻量级实现,方法与“信号量”基本一样。
class CSlimEvent
{
private:
SRWLOCK m_lock;
CONDITION_VARIABLE m_cv;
BOOL m_manual;
BOOL m_state;
public:
CSlimEvent( BOOL bManualReset, BOOL bInitialState )
{
InitializeSRWLock( &m_lock );
InitializeConditionVariable( &m_cv );
m_manual = bManualReset;
m_state = bInitialState;
}
~CSlimEvent() { }
BOOL Set()
{
AcquireSRWLockExclusive( &m_lock );
m_state = TRUE;
ReleaseSRWLockExclusive( &m_lock );
if( m_manual )
WakeAllConditionVariable( &m_cv );
else
WakeConditionVariable( &m_cv );
return TRUE;
}
BOOL Reset()
{
AcquireSRWLockExclusive( &m_lock );
m_state = FALSE;
ReleaseSRWLockExclusive( &m_lock );
return TRUE;
}
DWORD Wait( DWORD dwTimeout )
{
DWORD result = WAIT_TIMEOUT;
DWORD start = GetTickCount();
AcquireSRWLockExclusive( &m_lock );
while( true )
{
if( m_state )
{
m_state = m_manual;
result = WAIT_OBJECT_0;
break;
}
if( dwTimeout != INFINITE )
{
DWORD end = GetTickCount();
if( end - start >= dwTimeout )
break;
dwTimeout -= end - start;
start = end;
}
if( !SleepConditionVariableSRW( &m_cv, &m_lock, dwTimeout, 0 ) )
{
if( GetLastError() != ERROR_TIMEOUT )
result = WAIT_FAILED;
break;
}
}
ReleaseSRWLockExclusive( &m_lock );
return result;
}
};
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于