You can leave with a semaphore. A semaphore is basically an account, here you can allow developers to limit the number of threads / processes that access a certain resource. It usually works like
- You create a semaphore with a maximum number of N.
- N threads call a wait function on it,
WaitForSingleObjector the like, and each of them continues without waiting. Each time the internal semaphore counter goes down. - Thread N + 1 also calls the wait function, but since the internal counter of our semaphore is 0, it must wait.
- N ,
ReleaseSemaphore. . - , , 0.
, . , :
- , .
- , , . .
- ,
WaitForSingleObject(hSemaphore, 0), . 0 , .
.
++
HANDLER hSemaphore = CreateSemaphore(NULL, 0, BIG_NUMBER, "My cool semaphore name");
LONG prev_counter;
ReleaseSemaphore(hSemaphore, 1, &prev_counter);
WaitForSingleObject(hSemaphore, 0);
#
using System.Threading;
Semaphore sem = new Semaphore(0, BIG_NUMBER, "My cool semaphore name");
int prev_counter = sem.Release();
sem.WaitOne(0);
BIG_NUMBER .
, , mutex, .