title: Implementing a custom async task type and await
body: <p>I am developing a C++ app in which i need to receive messages from an MQ and then parsing them according to their type and for a particular reason I want to make this process (receiving a single message followed by processing it) asynchronous. Since, I want to keep things as simple as possible in a way that the next developer would have no problem continuing the code, I have written a very small class to implement Asynchrony.</p>
  <p>I first raise a new thread and pass a function to the thread:</p>
  <pre><code>task = new thread([&amp;] {
     result = fn();
      isCompleted = true;
  });
  task-&gt;detach();
  </code></pre>
  <p>and in order to await the task I do the following:</p>
  <pre><code>while (!isCompleted &amp;&amp; !(*cancelationToken))
  {
      Sleep(5);
  }
  state = 1; // marking the task as completed
  </code></pre>
  <p>So far there is no problem and I have not faced any bug or error but I am not sure if this is &quot;a good way to do this&quot; and my question is focused on determining this.</p>