SemaphoreSlim - sample code
Declare semaphore as below..
static SemaphoreSlim _sem = new SemaphoreSlim(4); // 4 people in
static int main()
{
for (int i = 1; i <= 10; i++) new Thread(Entered).Start(i);
}
static void Entered(object id)
{
Console.WriteLine(id + " wants to enter");
_sem.Wait();
Console.WriteLine(id + " is in!");
Thread.Sleep(1000);
Console.WriteLine(id + " is leaving");
_sem.Release();
}