Last active
August 11, 2020 17:46
-
-
Save RichardSilveira/2d77d0623685e1a667193c03b10b08b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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