title: How different is await/async from threading?
body: <p>I'm trying to familiarize myself with c#'s new await/async keywords, and I've found several aspects which I can't quite understand.</p>
  
  <ol>
  <li><p>Let's start with race conditions:</p>
  
  <pre><code>Stream s=...
  ...
  for(int i=0;i&lt;100;i++)
  {
      s.WriteAsync(new byte[]{i},0,1);
  }
  </code></pre>
  
  <p>will this work as expected all the time (e.g. write to the file 12345..... and not 13254 or something)? </p>
  
  <p>The second thing is that async function executes synchronously if it does not contain await operator. And, according to microsoft documentation, async functions always execute in the caller thread (as compared to BeginInvoke). This brings me to 3 next questions:</p></li>
  <li><p>How much of an async function is executed before it releases to the caller function?</p>
  
  <pre><code>async void MyAsyncFunction()
  {
      Operation1();
      Operation2();
      Operation3();
      ....
      Stream s=...;
      await s.WriteAsync(....);
  }
  </code></pre>
  
  <p>In the articles about await/async that I've read, it's said that async functions without await operator execute sequentially, and with async/await return imminently. But it's nagging at me that <code>MyAsyncFunction</code> may always execute Operation1...Operation3 before releasing as it hits <code>await s.WriteAsync</code>.</p></li>
  <li><p>What if I use <code>Thread.Sleep</code> in the async function like this:</p>
  
  <pre><code>async void DoStuff()
  {
      Stream s=...;
      ...
      await s.WriteAsync(....);
      Thread.Sleep(10000);
      ....
  }
  </code></pre>
  
  <p>Will Thread.Sleep block the whole thread in which it is executed or just the async function?</p></li>
  <li><p>What if I use <code>semaphore.Wait()</code> in one of the async functions and it will expect for the semaphore to be released by an other async function. Will this behave as it would with threads, or will it cause deadlock?</p></li>
  <li><p><code>await</code> does not work outside of async functions. Why?</p></li>
  </ol>