title: Throttling Tasks
body: <p>My service creates work in bursts and I would like to throttle the amount that happens concurrently. Assuming for simplicity that starvation of the work is not an issue, I am wondering if there is a difference from a perf perspective in simply leveraging the task scheduler + semaphore slim or doing this with a producer consumer.</p>
  
  <p>For instance, what are the pros/cons of this general idea:</p>
  
  <pre><code>        var throttlingSemaphore = new SemaphoreSlim(1, 1);
          for (int i = 0; i &lt; hugeNumber; i++)
          {
              var arg = i;
              Task.Run(
                  async () =&gt;
                  {
                      await throttlingSemaphore.WaitAsync();
                      DoWork(arg);
                  });
          }
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>           var workQueue = new AsyncQueue&lt;int&gt;();
              Task.Run(
                  async () =&gt;
                  {
                      while (_isAlive)
                      {
                          int arg = await workQueue.DequeueAsync();
                          DoWork(arg);
                      }
                  });
  
              for (int i = 0; i &lt; hugeNumber; i++)
              {
                  await workQueue.EnqueueAsync(i);
              }
  </code></pre>