{"id":6568,"date":"2023-05-29T19:18:57","date_gmt":"2023-05-29T19:18:57","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/dealing-with-mutation-guarded-suspension\/"},"modified":"2023-08-23T17:03:36","modified_gmt":"2023-08-23T17:03:36","slug":"dealing-with-mutation-guarded-suspension","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/dealing-with-mutation-guarded-suspension\/","title":{"rendered":"Dealing with Mutation: Guarded Suspension"},"content":{"rendered":"\n<p><\/p>\n\n\n<p>Guarded Suspension applies a unique strategy to deal with mutation. It signals when it is done with its modification.<\/p>\n<p><!--more--><\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6560\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/DealingWithMutation.png\" alt=\"\" width=\"650\" height=\"330\" data-alt=\"DealingWithMutation\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/DealingWithMutation.png 1234w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/DealingWithMutation-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/DealingWithMutation-1024x519.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/DealingWithMutation-768x390.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The guarded suspension basic variant combines a lock and a precondition that must be satisfied. If the precondition is not fulfilled, that checking thread puts itself to sleep. The checking thread uses a lock to avoid a race condition that may result in a data race or a deadlock.<\/p>\n<p>Various variants of the Guarded Suspension exist:<\/p>\n<ul>\n<li>The waiting thread can passively be notified about the state change or actively ask for the state change. In short, I call this push versus pull principle.<\/li>\n<li>The waiting can be done with or without a time boundary.<\/li>\n<li>The notification can be sent to one or all waiting threads.<\/li>\n<\/ul>\n<p>I present in this post only the rough idea. For further information, I refer to posts I have already written.<\/p>\n<h2>Push versus Pull Principle<\/h2>\n<p>Let me start with the push principle.<\/p>\n<h3>Push Principle<\/h3>\n<p>You often synchronize threads with a <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/condition-variable\">condition variable<\/a> or a <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/tasks\">future\/promise <\/a>pair. The condition variable or the promise sends the notification to the waiting thread. A promise has no <code>notify_one<\/code> or <code>notify_all<\/code> member function. Typically, a valueless <code>set_value<\/code> call is used to signal a notification. The following program snippets show the thread sending the notification and the waiting thread.<\/p>\n<ul>\n<li>Condition Variable<\/li>\n<\/ul>\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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">waitingForWork<\/span>(){\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Worker: Waiting for work.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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_);\n    condVar.wait(lck, []{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> dataReady; });\n    doTheWork();\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Work done.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">setDataReady<\/span>(){\n    {\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_);\n      dataReady <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;\n    }\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Sender: Data is ready.\"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    condVar.notify_one();\n}\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<ul>\n<li>Future\/Promise Pair<\/li>\n<\/ul>\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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">waitingForWork<\/span>(std<span style=\"color: #555555;\">::<\/span>future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> fut){\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Worker: Waiting for work.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    fut.wait();\n    doTheWork();\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Work done.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">setDataReady<\/span>(std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> prom){\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Sender: Data is ready.\"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    prom.set_value();\n\n}\n<\/pre>\n<\/div>\n<h3>Pull Principle<\/h3>\n<p>Instead of passively waiting for the state change, you can actively ask for it. This pull principle is not natively supported in C++ but can be, for example, implemented with <a href=\"https:\/\/www.modernescpp.com\/index.php\/atomics\">atomics<\/a>.<\/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%;\">std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> mySharedWork;\nstd<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">bool<\/span><span style=\"color: #555555;\">&gt;<\/span> dataReady(<span style=\"color: #336666;\">false<\/span>);\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">waitingForWork<\/span>(){\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>;\n    <span style=\"color: #006699; font-weight: bold;\">while<\/span> (<span style=\"color: #555555;\">!<\/span>dataReady.load()){                \n        std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>milliseconds(<span style=\"color: #ff6600;\">5<\/span>));\n    }\n    mySharedWork[<span style=\"color: #ff6600;\">1<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;                     \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Work done \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">setDataReady<\/span>(){\n    mySharedWork <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">3<\/span>};                  \n    dataReady <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;                          \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Data prepared\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<h2>Waiting with and without Time Boundary<\/h2>\n<p>A condition variable and a future have three member functions for waiting: <code>wait,<\/code> <code>wait_for<\/code>, and <code>wait_until<\/code>. The <code>wait_for<\/code> variant requires a time duration, and the <code>wait_until<\/code> variant a time point.<\/p>\n<p>The consumer thread waits for the time duration <code>steady_clock::now() + dur. T<\/code>he future asks for the value; if the promise is not done, it displays its id: t<code>his_thread::get_it()<\/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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">producer<\/span>(promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> prom){\n    cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"PRODUCING THE VALUE 2011<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>; \n    this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(seconds(<span style=\"color: #ff6600;\">5<\/span>));\n    prom.set_value(<span style=\"color: #ff6600;\">2011<\/span>);\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">consumer<\/span>(shared_future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> fut,\n              steady_clock<span style=\"color: #555555;\">::<\/span>duration dur){\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> steady_clock<span style=\"color: #555555;\">::<\/span>now();\n    future_status status<span style=\"color: #555555;\">=<\/span> fut.wait_until(steady_clock<span style=\"color: #555555;\">::<\/span>now() <span style=\"color: #555555;\">+<\/span> dur);\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( status <span style=\"color: #555555;\">==<\/span> future_status<span style=\"color: #555555;\">::<\/span>ready ){\n        lock_guard<span style=\"color: #555555;\">&lt;<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lockCout(coutMutex);\n        cout <span style=\"color: #555555;\">&lt;&lt;<\/span> this_thread<span style=\"color: #555555;\">::<\/span>get_id() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" ready =&gt; Result: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> fut.get() \n             <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    }\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\n        lock_guard<span style=\"color: #555555;\">&lt;<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lockCout(coutMutex);\n        cout <span style=\"color: #555555;\">&lt;&lt;<\/span> this_thread<span style=\"color: #555555;\">::<\/span>get_id() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" stopped waiting.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    }\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> end<span style=\"color: #555555;\">=<\/span> steady_clock<span style=\"color: #555555;\">::<\/span>now();\n    lock_guard<span style=\"color: #555555;\">&lt;<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lockCout(coutMutex);\n    cout <span style=\"color: #555555;\">&lt;&lt;<\/span> this_thread<span style=\"color: #555555;\">::<\/span>get_id() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" waiting time: \"<\/span> \n         <span style=\"color: #555555;\">&lt;&lt;<\/span> getDifference(start,end) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" ms\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n<\/pre>\n<\/div>\n<h2>Notifying one or all waiting Threads<\/h2>\n<p><code>notify_one<\/code> awakes one of the waiting threads, <code>notify_all<\/code> awakes all the waiting threads. With <code>notify_one<\/code>, you have no guarantee which one will be awakened. The other threads do stay in the wait state. This could not happen with a <code>std::future<\/code>, because there is a one-to-one association between the future and the promise. If you want to simulate a one-to-many association, use a <code>std::shared_future<\/code> instead of a <code>std::future<\/code> because a <code>std::shared_future<\/code> can be copied.<\/p>\n<p>The following program shows a simple workflow with one-to-one and one-to-many associations between promises and futures.<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ bossWorker.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;future&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;random&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">getRandomTime<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> start, <span style=\"color: #007788; font-weight: bold;\">int<\/span> end){\n  \n  std<span style=\"color: #555555;\">::<\/span>random_device seed;\n  std<span style=\"color: #555555;\">::<\/span>mt19937 engine(seed());\n  std<span style=\"color: #555555;\">::<\/span>uniform_int_distribution<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> dist(start,end);\n  \n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> dist(engine);\n};\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Worker<\/span>{\n<span style=\"color: #9999ff;\">public:<\/span>\n  <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Worker(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> n)<span style=\"color: #555555;\">:<\/span>name(n){};\n  \n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">operator<\/span>() (std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> preparedWork, \n                   std<span style=\"color: #555555;\">::<\/span>shared_future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> boss2Worker){\n      \n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ prepare the work and notfiy the boss<\/span>\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> prepareTime<span style=\"color: #555555;\">=<\/span> getRandomTime(<span style=\"color: #ff6600;\">500<\/span>, <span style=\"color: #ff6600;\">2000<\/span>);\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>milliseconds(prepareTime));\n    preparedWork.set_value();                                 <em><span style=\"color: #0099ff;\"> \/\/ (5)<\/span> <\/em>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> name <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\": \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Work prepared after \"<\/span> \n              <span style=\"color: #555555;\">&lt;&lt;<\/span> prepareTime <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" milliseconds.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ still waiting for the permission to start working<\/span>\n    boss2Worker.wait();\n  }    \n<span style=\"color: #9999ff;\">private:<\/span>\n  std<span style=\"color: #555555;\">::<\/span>string name;\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ define the std::promise =&gt; Instruction from the boss<\/span>\n  std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> startWorkPromise;\n\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the std::shared_future's from the std::promise<\/span>\n  std<span style=\"color: #555555;\">::<\/span>shared_future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> startWorkFuture<span style=\"color: #555555;\">=<\/span> startWorkPromise.get_future();\n\n  std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> herbPrepared;\n  std<span style=\"color: #555555;\">::<\/span>future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> waitForHerb <span style=\"color: #555555;\">=<\/span> herbPrepared.get_future();\n  Worker herb(<span style=\"color: #cc3300;\">\"  Herb\"<\/span>);                                           <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span> <\/em>\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> herbWork(herb, std<span style=\"color: #555555;\">::<\/span>move(herbPrepared), startWorkFuture);\n  \n  std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> scottPrepared;\n  std<span style=\"color: #555555;\">::<\/span>future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> waitForScott <span style=\"color: #555555;\">=<\/span> scottPrepared.get_future();\n  Worker scott(<span style=\"color: #cc3300;\">\"    Scott\"<\/span>);                                      <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span> <\/em>\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> scottWork(scott, std<span style=\"color: #555555;\">::<\/span>move(scottPrepared), startWorkFuture);\n  \n  std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> bjarnePrepared;\n  std<span style=\"color: #555555;\">::<\/span>future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">&gt;<\/span> waitForBjarne <span style=\"color: #555555;\">=<\/span> bjarnePrepared.get_future();\n  Worker bjarne(<span style=\"color: #cc3300;\">\"      Bjarne\"<\/span>);                                  <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> bjarneWork(bjarne, std<span style=\"color: #555555;\">::<\/span>move(bjarnePrepared), startWorkFuture);\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"BOSS: PREPARE YOUR WORK.<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\"> \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ waiting for the worker <\/span>\n  waitForHerb.wait(), waitForScott.wait(), waitForBjarne.wait();  <em><span style=\"color: #0099ff;\">\/\/ (4)\n <\/span> <\/em>\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ notify the workers that they should begin to work<\/span>\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">BOSS: START YOUR WORK. <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  startWorkPromise.set_value();                                   <em><span style=\"color: #0099ff;\">\/\/ (6)<\/span><\/em>\n  \n  herbWork.join();\n  scottWork.join();\n  bjarneWork.join();\n   \n}\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>The key idea of the program is that the <code>boss<\/code> (main-thread) has three workers: <code>herb<\/code> (line 1), <code>scott<\/code> (line 3), and <code>bjarne<\/code> (line 3). A thread represents each worker. In line (4), the boss waits until all workers complete their work package preparation. This means each worker sends, after an arbitrary time, the notification to the boss that he is done. The worker-to-the-boss notification is a one-to-one relation because it uses a <code>std::future<\/code> (line 5). In contrast, the instruction to start the work is a one-to-many notification (line 6) from the boss to its workers. For this one-to-many notification, a <code>std::shared_future<\/code> is necessary.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4738\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker.png\" alt=\"bossWorker\" width=\"400\" height=\"206\" \/><\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>In my next post, I will focus on concurrent architecture and write about the Active Object.<\/p>\n<h2>A Short Break<\/h2>\n<p>I will take a short two weeks holiday break. My next post will be published on Monday, the 19th of June.<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":21,"featured_media":6560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[451,430,433],"class_list":["post-6568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-condition-variables","tag-lock","tag-mutex"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6568","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=6568"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6568\/revisions"}],"predecessor-version":[{"id":7056,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6568\/revisions\/7056"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6560"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}