{"id":6087,"date":"2021-02-12T10:28:52","date_gmt":"2021-02-12T10:28:52","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/an-improved-thread-with-c-20\/"},"modified":"2021-02-12T10:28:52","modified_gmt":"2021-02-12T10:28:52","slug":"an-improved-thread-with-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/an-improved-thread-with-c-20\/","title":{"rendered":"An Improved Thread with C++20"},"content":{"rendered":"<p><code>std::jthread<\/code> stands for joining thread. In addition to <code>std::thread<\/code> (C++11),<code> std::jthread<\/code> automatically joins in its destructor and can cooperatively be interrupted. Read this post to know why <code>std::jthread<\/code> should be your first choice.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5199\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/02\/TimelineCpp20.png\" alt=\"TimelineCpp20\" width=\"650\" height=\"227\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>The following table gives you a concise overview of the functionality of <code>std::jthread<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5532\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/jthread.png\" alt=\"jthread\" width=\"650\" height=\"572\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>For additional details, please refer to <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/thread\/jthread\">cppreference.com<\/a>. When you want to read more posts about <code>std::thread<\/code>, here are they: <a href=\"https:\/\/www.modernescpp.com\/index.php\/der-einstieg-in-modernes-c#h1-2-the-threading-interface\">my post about std::thread.<\/a><\/p>\n<p>First, why do we need an improved thread in C++20? Here is the first reason.<\/p>\n<\/p>\n<h2>Automatically Joining<\/h2>\n<p>This is the <strong>non-intuitive<\/strong> behavior of <code>std::thread<\/code>. If a <code>std::thread<\/code> is still joinable, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/error\/terminate\">std::terminate<\/a> is called in its destructor. A thread<code> thr<\/code> is joinable if neither <code>thr.join()<\/code> nor <code>thr.detach()<\/code> was called. Let me show what that means.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ threadJoinable.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> thr{[]{ std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Joinable std::thread\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; }};\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"thr.joinable(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> thr.joinable() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When executed, the program terminates when the local object <code>thr<\/code> goes out of scope.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5528\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/threadJoinable.png\" alt=\"threadJoinable\" width=\"500\" height=\"279\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/threadJoinable.png 644w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/threadJoinable-300x167.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Both executions of<code> std::thread<\/code> terminate. In the second run, the thread <code>thr<\/code> has enough time to display its message:&nbsp;<code>Joinable std::thread<\/code>.<\/p>\n<p>In the next example, I use <code>std::jthread<\/code> from the C++20 standard.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ jthreadJoinable.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>jthread thr{[]{ std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Joinable std::thread\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; }};\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"thr.joinable(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> thr.joinable() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, the thread<code> thr<\/code> automatically joins in its destructor if it&#8217;s still joinable such as in this case.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5529\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/jthreadJoinable.png\" alt=\"jthreadJoinable\" width=\"350\" height=\"177\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/jthreadJoinable.png 443w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/jthreadJoinable-300x152.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>But this is not all that<code> std::jthread<\/code>&nbsp; provides additionally to <code>std::thread<\/code>. A <code>std::jthread<\/code> can be cooperatively interrupted. I already presented the general ideas of cooperative interruption in my last post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/cooperative-interruption-of-a-thread-in-c-20\">Cooperative Interruption of a Thread in C++20<\/a>.<\/p>\n<h2>Cooperative Interruption of a<code> std::jthread<\/code><\/h2>\n<p>To get a general idea, let me present a simple example.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ interruptJthread.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span><span style=\"color: #555555;\">::<\/span>std<span style=\"color: #555555;\">::<\/span>literals;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>jthread nonInterruptable([]{                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #007788; font-weight: bold;\">int<\/span> counter{<span style=\"color: #ff6600;\">0<\/span>};\r\n        <span style=\"color: #006699; font-weight: bold;\">while<\/span> (counter <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">10<\/span>){\r\n            std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">0.2<\/span>s);\r\n            std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"nonInterruptable: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> counter <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \r\n            <span style=\"color: #555555;\">++<\/span>counter;\r\n        }\r\n    });\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>jthread interruptable([](std<span style=\"color: #555555;\">::<\/span>stop_token stoken){     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n        <span style=\"color: #007788; font-weight: bold;\">int<\/span> counter{<span style=\"color: #ff6600;\">0<\/span>};\r\n        <span style=\"color: #006699; font-weight: bold;\">while<\/span> (counter <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">10<\/span>){\r\n            std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">0.2<\/span>s);\r\n            <span style=\"color: #006699; font-weight: bold;\">if<\/span> (stoken.stop_requested()) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n            std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"interruptable: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> counter <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \r\n            <span style=\"color: #555555;\">++<\/span>counter;\r\n        }\r\n    });\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Main thread interrupts both jthreads\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    nonInterruptable.request_stop();\r\n    interruptable.request_stop();                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the main program, I start the two threads <code>nonInterruptable<\/code> and interruptable (lines 1)and 2). Unlike in the thread <code>nonInterruptable<\/code> , the thread <code>interruptable<\/code> gets a <code>std::stop_token<\/code> and uses it in line (3) to check if it was interrupted: <code>stoken.stop_requested()<\/code>. In case of a stop request, the lambda function returns, and the thread ends. The call <code>interruptable.request_stop()<\/code> (line 4) triggers the stop request. This does not hold for the previous call <code>nonInterruptable.request_stop()<\/code> . The call has no effect.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5530\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/interruptJthread.png\" alt=\"interruptJthread\" width=\"400\" height=\"382\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/interruptJthread.png 735w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/interruptJthread-300x287.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>To complete my post, with C++20, you can also cooperatively interrupt a condition variable.<\/p>\n<h2>New wait Overloads for <code>std::condition_variable_any<\/code><\/h2>\n<p>Before I write about <code>std::condition_variable_any<\/code>, here are my post about <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/condition-variable\">condition variables<\/a>.&nbsp;<\/p>\n<p>The three wait variations <code>wait<\/code>,<code> wait_for<\/code>, and<code> wait_until<\/code> of the std::condition_variable_any get new overloads. These overloads take a <code>std::stop_token<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Predicate<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> wait(Lock<span style=\"color: #555555;\">&amp;<\/span> lock,  \r\n          stop_token stoken,\r\n          Predicate pred);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Rep<\/span>, <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Period<\/span>, <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Predicate<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> wait_for(Lock<span style=\"color: #555555;\">&amp;<\/span> lock, \r\n              stop_token stoken, \r\n              <span style=\"color: #006699; font-weight: bold;\">const<\/span> chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span>Rep, Period<span style=\"color: #555555;\">&gt;&amp;<\/span> rel_time, \r\n              Predicate pred);\r\n                \r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Clock<\/span>, <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Duration<\/span>, <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Predicate<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> wait_until(Lock<span style=\"color: #555555;\">&amp;<\/span> lock, \r\n                stop_token stoken,\r\n                <span style=\"color: #006699; font-weight: bold;\">const<\/span> chrono<span style=\"color: #555555;\">::<\/span>time_point<span style=\"color: #555555;\">&lt;<\/span>Clock, Duration<span style=\"color: #555555;\">&gt;&amp;<\/span> abs_time, \r\n                Predicate pred);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>These new overloads need a predicate. The presented versions ensure getting notified if a stop request for the passed <code>std::stop_token stoken<\/code> is signaled. They return a boolean that indicates whether the predicate evaluates to <code>true<\/code>. This returned boolean is independent of whether a stop was requested or whether the timeout was triggered.<\/p>\n<p>After the wait calls, you can check if a stop request occurred.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">cv.wait(lock, stoken, predicate);\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (stoken.stop_requested()){\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ interrupt occurred<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The following example shows the usage of a condition variable with a stop request.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ conditionVariableAny.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;condition_variable&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;mutex&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>literals;\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>mutex mutex_;\r\nstd<span style=\"color: #555555;\">::<\/span>condition_variable_any condVar;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> dataReady;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">receiver<\/span>(std<span style=\"color: #555555;\">::<\/span>stop_token stopToken) {                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Waiting\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>unique_lock<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lck(mutex_);\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> ret <span style=\"color: #555555;\">=<\/span> condVar.wait(lck, stopToken, []{<span style=\"color: #006699; font-weight: bold;\">return<\/span> dataReady;});\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (ret){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Notification received: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n         std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Stop request received\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">sender<\/span>() {                                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">5<\/span>ms);\r\n    {\r\n        std<span style=\"color: #555555;\">::<\/span>lock_guard<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lck(mutex_);\r\n        dataReady <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Send notification\"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n    condVar.notify_one();                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>jthread t1(receiver);\r\n  std<span style=\"color: #555555;\">::<\/span>jthread t2(sender);\r\n  \r\n  t1.request_stop();                                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n  t1.join();\r\n  t2.join();\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The receiver thread (line 1) is waiting for the notification of the sender thread (line 2). Before the sender thread sends its notification (line 3), the main thread triggers a stop request in line (4). The program&#8217;s output shows that the stop request happened before the notification.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6086\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/conditionVariableAny.png\" alt=\"conditionVariableAny\" width=\"281\" height=\"113\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/h2>\n<h2>What&#8217;s next?<\/h2>\n<p>What happens when your write without synchronization to <code>std::cout<\/code>? You get a mess. Thanks to C++20, we have synchronized output streams.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<div style=\"color: #000000; background-color: #ffffff; font-family: Consolas, 'Courier New', monospace; font-weight: normal; font-size: 18px; line-height: 24px; white-space: pre;\"><code>&nbsp;<\/code><\/div>\n","protected":false},"excerpt":{"rendered":"<p>std::jthread stands for joining thread. In addition to std::thread (C++11), std::jthread automatically joins in its destructor and can cooperatively be interrupted. Read this post to know why std::jthread should be your first choice.<\/p>\n","protected":false},"author":21,"featured_media":5199,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[],"class_list":["post-6087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6087"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6087\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5199"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}