Reputation: 2735
I am trying to create a simple semaphore that will allow only 1 thread to increment a count variable but what I am currently doing does not seem to work. Is there something wrong with my syntax or am I completely on the wrong track? (I named the semaphore mutex, because it can only have a value of 0 or 1)
WaitForSingleObject(mutex, 0);
count = count + 1;
ReleaseSemaphore(mutex, 1, NULL);
Upvotes: 0
Views: 1216
Reputation: 28829
Try INFINITE
rather than zero as your second argument to WaitForSingleObject
.
Upvotes: 0
Reputation: 48280
From the MSDN page, with emphasis added:
DWORD WINAPI WaitForSingleObject( __in HANDLE hHandle, __in DWORD dwMilliseconds );
dwMilliseconds [in]
The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the object is signaled or the interval elapses. If dwMilliseconds is zero, the function does not enter a wait state if the object is not signaled; it always returns immediately. If dwMilliseconds isINFINITE
, the function will return only when the object is signaled.
Upvotes: 2