title: Sleep an Async Task
body: <p>In my project I need to poll some devices every n seconds and sleep and continue forever. I have created an async task with launch as async instead of <code>std::thread</code>. But if I use <code>std::this_thread::sleep_for()</code> inside an async task with launch as async, it looks like its actually blocking my main thread?
  The following program outputs "Inside Async.." forever, it never prints "Main function".</p>
  
  <p>Instead of async, if I use a <code>std::thread()</code>, it would work fine. But I wanted to use an async task as I don't have to join it and manage its lifetime unlike a thread.</p>
  
  <p>How do I make an async task sleep?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;future&gt;
  #include &lt;thread&gt;
  
  int main() 
  {
      std::async(std::launch::async,
      []()
      {
          while(true)
          {
              std::cout &lt;&lt;"Inside async.."&lt;&lt; std::endl;   
              std::this_thread::sleep_for(std::chrono::seconds(2));
          }
      });
  
      std::cout &lt;&lt;"Main function"&lt;&lt; std::endl;
      return 0;
  }
  </code></pre>