Public · Protected · Private
SemaphoreSlim - sample code
Type: Public  |  Created: 2012-07-19  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • Semaphore 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(); }
    2012-07-19 11:10
This blog is frozen. No new comments or edits allowed.