title: Benefits of using std::stop_source and std::stop_token instead of std::atomic<bool> for deferred cancellation?
body: <p>When I run several std::threads in parallell and need to cancel other threads in a deferred manner if one thread fails I use a <code>std::atomic&lt;bool&gt;</code> flag:</p>
  <pre><code>#include &lt;thread&gt;
  #include &lt;chrono&gt;
  #include &lt;iostream&gt;
  
  void threadFunction(unsigned int id, std::atomic&lt;bool&gt;&amp; terminated) {
      srand(id);
      while (!terminated) {
          int r = rand() % 100;
          if (r == 0) {
              std::cerr &lt;&lt; &quot;Thread &quot; &lt;&lt; id &lt;&lt; &quot;: an error occured.\n&quot;;
              terminated = true; // without this line we have to wait for other thread to finish
              return;
          }
          std::this_thread::sleep_for(std::chrono::milliseconds(100));
      }
  }
  
  int main()
  {
      std::atomic&lt;bool&gt; terminated = false;
      std::thread t1(&amp;threadFunction, 1, std::ref(terminated));
      std::thread t2(&amp;threadFunction, 2, std::ref(terminated));
  
      t1.join();
      t2.join();
      std::cerr &lt;&lt; &quot;Both threads finished.\n&quot;;
      int k;
      std::cin &gt;&gt; k;
  }
  </code></pre>
  <p>However now I am reading about <code>std::stop_source</code>and <code>std::stop_token</code>.
  I find that I can achieve the same as above by passing both a <code>std::stop_source</code>by reference and <code>std::stop_token</code> by value to the thread function?
  How would that be superior?</p>
  <p>I understand that when using <code>std::jthread</code> the <code>std::stop_token</code> is very convenient if I want to stop threads from outside the threads.
  I could then call <code>std::jthread::request_stop()</code> from the main program.</p>
  <p>However in the case where I want to stop threads from a thread is it still better?</p>
  <p>I managed to achieve the same thing as in my code using <code>std::stop_source</code>:</p>
  <pre><code> void threadFunction(std::stop_token stoken, unsigned int id, std::stop_source source) {
      srand(id);
      while (!stoken.stop_requested()) {
          int r = rand() % 100;
          if (r == 0) {
              std::cerr &lt;&lt; &quot;Thread &quot; &lt;&lt; id &lt;&lt; &quot;: an error occured.\n&quot;;
              source.request_stop(); // without this line we have to wait for other thread to finish
              return;
          }
          std::this_thread::sleep_for(std::chrono::milliseconds(100));
      }
  }
  int main()
  {
      std::stop_source source;
      std::stop_token stoken = source.get_token();
      std::thread t1(&amp;threadFunction, stoken, 1, source);
      std::thread t2(&amp;threadFunction, stoken, 2, source);
      t1.join();
      t2.join();
      std::cerr &lt;&lt; &quot;Both threads finished.\n&quot;;
      int k;
      std::cin &gt;&gt; k;
  }
  </code></pre>
  <p>Using <code>std::jthread</code> would have resulted in more compact code:</p>
  <pre><code>std::jthread t1(&amp;threadFunction, 1, source);
  std::jthread t2(&amp;threadFunction, 2, source);
  </code></pre>
  <p>But that did not seem to work.</p>