We can use either the new condition variable primitive or windows event in order to synchronize threads in WinNT v6.x or later. Consider the following two approaches, we want workers to run at the same time when "go" is set in main, otherwise they should all block.
/*language C code*/
/*Windows Condition Variable*/
int go=0;
CONDITION_VARIABLE cv;
SRWLOCK lock;
void workers()
{
AcquireSRWLockShared(&lock);
if(go==0)
{
SleepConditionVariableSRW(&cv, &lock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED);
}
ReleaseSRWLockShared(&lock);
/*
Workers continue...
*/
}
void main()
{
int i;
InitializeConditionVariable(&cv);
InitializeSRWLock(&lock);
for(i=0;i<10;i++)
{
CreateThread(0, 0, workers, 0, 0, 0);
}
AcquireSRWLockExclusive(&lock);
go=1;
ReleaseSRWLockExclusive(&lock);
WakeAllConditionVariable(&cv);
}
or
/*language C code*/
/*Windows Event*/
HANDLE go;
void workers()
{
WaitForSingleObject(go, INFINITE);
/*
Workers continue...
*/
}
void main()
{
int i;
go=CreateEvent(0,1,0,0); /*No security descriptor, Manual Reset, initially 0, no name*/
for(i=0;i<10;i++)
{
CreateThread(0, 0, workers, 0, 0, 0);
}
SetEvent(go);
}
In the first approach, workers are blocked on SleepConditionVariableSRW and woke up by WakeAllConditionVariable. In the second, they are blocked on WaitForSingleObject and woke up by SetEvent.
Which one is better in practice, only regarding overhead? (hint: context switch, lock contention, overhead of blocking threads)
I would choose the first but feel lack of justification.