title: How to have mutliple threads await a single Task?
body: <p>I've read this: <a href="https://stackoverflow.com/questions/16820388/is-it-ok-to-await-the-same-task-from-multiple-threads-is-await-thread-safe">Is it ok to await the same task from multiple threads - is await thread safe?</a> and I don't feel clear about the answer, so here's a specific use case.</p>
  
  <p>I have a method that performs some async network I/O. Multiple threads can hit this method at once, and I dont wan't them all to invoke a network request, If a request is already in progress I want to block/await the 2nd+ threads, and have them all resume once the single IO operation has completed.</p>
  
  <p>How <em>should</em> I write the following pseudcode?
  I'm guessing each calling thread really needs to get its own <code>Task</code>, so each can get it's own continuation, so instead of returning <code>currentTask</code> I should return a new <code>Task</code> which is completed by the "inner" <code>Task</code> from <code>DoAsyncNetworkIO</code>.
  Is there a clean way to do this, or do I have to hand roll it?</p>
  
  <pre><code>static object mutex = new object();
  static Task currentTask;
  
  async Task Fetch()
  {
      lock(mutex)
      {
          if(currentTask != null)
              return currentTask;
      }
  
      currentTask = DoAsyncNetworkIO();
      await currentTask;
  
      lock(mutex)
      {
          var task = currentTask;
          currentTask = null;
          return task;
      }
  }
  </code></pre>