title: Implement Semaphore using Monitor.Wait(obj) and Monitor.Pulse(obj)
body: <p>I have a following task:</p>
  <p>&quot;Ordinary&quot; semaphore</p>
  <p>Initialized to the number of available resources.</p>
  <p>Operations:</p>
  <pre><code>request () - waits until the resource is released,
  release () - releases the resource,
  numberAvailable () - returns number available resources, the n  
  </code></pre>
  <p>which I tried to implement using <code>Semaphore</code> slim:</p>
  <pre><code>    class Program
      {
          private const int InitialCount = 2;
          private const int MaximumCount = 8;
  
          static void Main()
          {
              var semaphoreObject = new SemaphoreSlim(InitialCount, MaximumCount);
              var printerObject = new Printer();
  
              for (int i = 0; i &lt; 20; ++i)
              {
                  int j = i;
                  Task.Factory.StartNew(() =&gt;
                  {
                      Request(semaphoreObject);
                      printerObject.Print(j);
                      Release(semaphoreObject);
                      NumberAvailable(semaphoreObject);
                  });
              }
              Console.ReadLine();
          }
  
          private static void Request(SemaphoreSlim s)
          {
              s.Wait();
          }
  
          private static void Release(SemaphoreSlim s)
          {
              s.Release();
          }
  
          private static void NumberAvailable(SemaphoreSlim s)
          {
              Console.WriteLine($&quot;Available resource: {s.CurrentCount}&quot;);
          }
      }
  
      class Printer
      {
          public void Print(int documentToPrint)
          {
              Console.WriteLine(&quot;Printing document: &quot; + documentToPrint);
              Thread.Sleep(TimeSpan.FromSeconds(5));
          }
      }
  </code></pre>
  <p>However now, I need to do that using Monitor class, so I should create my own semaphore class. However I am a bit lost on how to change that</p>