Skip to content

Instantly share code, notes, and snippets.

@RichardSilveira
Last active August 11, 2020 17:46
Show Gist options
  • Save RichardSilveira/2d77d0623685e1a667193c03b10b08b7 to your computer and use it in GitHub Desktop.
Save RichardSilveira/2d77d0623685e1a667193c03b10b08b7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using static System.Console;
namespace ThreadPlayground
{
class NonExclusiveLockSemaphore
{
private static SemaphoreSlim _semaphore = new SemaphoreSlim(3);
internal static void Start()
{
for (int i = 0; i <= 6; i++)
{
new Thread(EnterInClub).Start(i);
}
}
private static void EnterInClub(object id)
{
WriteLine($"{id} wants to enter");
_semaphore.Wait();
WriteLine($"{id} walked in");
Thread.Sleep(2000 * (int)id);
WriteLine($"{id} is leaving already");
_semaphore.Release();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment