Musings
Public · Protected · Private
SemaphoreSlim - sample code
-
2012-07-19 11:10Semaphore allowes specified number of threads inside. -- 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(); }
This blog is frozen. No new comments or edits allowed.