{"id":6559,"date":"2023-05-08T10:12:08","date_gmt":"2023-05-08T10:12:08","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/dealing-with-sharing\/"},"modified":"2023-05-08T10:12:08","modified_gmt":"2023-05-08T10:12:08","slug":"dealing-with-sharing","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/dealing-with-sharing\/","title":{"rendered":"Dealing with Sharing"},"content":{"rendered":"<p>If you don\u2019t share, no data races can happen. Not sharing means that your thread works on local variables. This can be achieved by copying the value, using thread-specific storage, or transferring the result of a thread to its associated future via a protected data channel.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6554\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/Sharing.png\" alt=\"Sharing\" width=\"650\" height=\"324\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/Sharing.png 1231w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/Sharing-300x150.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/Sharing-1024x511.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/Sharing-768x383.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The patterns in this section are quite obvious, but I will present them with a short explanation for completeness. Let me start with Copied Value.<\/p>\n<h2>Copied Value<\/h2>\n<p>If a thread gets its arguments by copy and not by reference, there is no need to synchronize access to any data. No data races and no lifetime issues are possible.<\/p>\n<h3>Data Races with References<\/h3>\n<p>The following program creates three threads. One thread gets its argument by copy, the other by reference, and the last by constant reference.<\/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;\">\/\/ copiedValueDataRace.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&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>chrono_literals;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">byCopy<\/span>(<span style=\"color: #007788; font-weight: bold;\">bool<\/span> b){\r\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>ms);             <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"byCopy: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">byReference<\/span>(<span style=\"color: #007788; font-weight: bold;\">bool<\/span><span style=\"color: #555555;\">&amp;<\/span> b){\r\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>ms);            <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"byReference: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">byConstReference<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span><span style=\"color: #555555;\">&amp;<\/span> b){\r\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>ms);            <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"byConstReference: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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> std<span style=\"color: #555555;\">::<\/span>boolalpha <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> shared{<span style=\"color: #336666;\">false<\/span>};\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t1(byCopy, shared);\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t2(byReference, std<span style=\"color: #555555;\">::<\/span>ref(shared));\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t3(byConstReference, std<span style=\"color: #555555;\">::<\/span>cref(shared));\r\n    \r\n    shared <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;\r\n    \r\n    t1.join();\r\n    t2.join();\r\n    t3.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>Each thread sleeps for one millisecond (lines 1, 2, and 3) before displaying the boolean value. Only the thread <code>t1<\/code> has a local copy of the boolean and has, therefore, no data race. The program\u2019s output shows that the boolean values of threads <code>t2<\/code> and<code> t3<\/code> are modified without synchronization.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6555\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueDataRace.png\" alt=\"copiedValueDataRace\" width=\"500\" height=\"369\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueDataRace.png 616w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueDataRace-300x221.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>You may think that the thread t3 in the previous example<code> copiedValueDataRace.cpp<\/code> can just be replaced with<code> std::thread t3(byConstReference, shared<\/code>). The program compiles and runs, but what seems like a reference is a copy. The reason is that the type traits function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/decay\">std::decay<\/a> is applied to each thread argument. <code>std::decay<\/code> performs lvalue-to-rvalue, array-to-pointer, and function-to-pointer implicit conversions to its type<code> T<\/code>. In particular, it invokes, in this case, the type traits function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/decay\"><code>std::remove_reference<\/code><\/a> on the type <code>T<\/code>.<\/p>\n<p>The following program <code>perConstReference.cpp<\/code> uses a non-copyable type <code>NonCopyableClass<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ perConstReference.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">NonCopyableClass<\/span>{\r\n    <span style=\"color: #9999ff;\">public:<\/span>\r\n\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ the compiler generated default constructor<\/span>\r\n    NonCopyableClass() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ disallow copying<\/span>\r\n    NonCopyableClass<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #006699; font-weight: bold;\">const<\/span> NonCopyableClass<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n    NonCopyableClass (<span style=\"color: #006699; font-weight: bold;\">const<\/span> NonCopyableClass<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n  \r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">perConstReference<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> NonCopyableClass<span style=\"color: #555555;\">&amp;<\/span> nonCopy){}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n    NonCopyableClass nonCopy;                      <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n\r\n    perConstReference(nonCopy);                    <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t(perConstReference, nonCopy);    <em><span style=\"color: #0099ff;\"> \/\/ (3)<\/span><\/em>\r\n    t.join();\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The object <code>nonCopy<\/code> (line 1) is not copyable. This is fine if I invoke the function<code> perConstReference<\/code> with the argument <code>nonCopy<\/code> (line 2) because the function accepts its argument per constant reference. Using the same function in the thread <code>t<\/code> (line 3) causes GCC to generate a verbose compiler error with more than 300 lines:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6556\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/numberOfLinesPerConstReference.png\" alt=\"numberOfLinesPerConstReference\" width=\"650\" height=\"116\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/numberOfLinesPerConstReference.png 972w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/numberOfLinesPerConstReference-300x54.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/numberOfLinesPerConstReference-768x137.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The error message\u2019s essential part is in the middle of the screenshot in red rounded rectangle: \u201c<code>error: use of deleted function<\/code>\u201d. The copy-constructor of the class <code>NonCopyableClass<\/code> is not available.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6557\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/perConstReference.png\" alt=\"perConstReference\" width=\"650\" height=\"332\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/perConstReference.png 1153w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/perConstReference-300x153.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/perConstReference-1024x523.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/perConstReference-768x392.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>When you borrow something, you have to ensure that the underlying value is still available when you use it.<\/p>\n<\/p>\n<h3>Lifetime Issues with References<\/h3>\n<p>If a thread uses its argument by reference and you detach the thread, you have to be extremely careful. The small program <code>copiedValueLifetimeIssues.cpp<\/code> has undefined behavior.<\/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;\">\/\/ copiedValueLifetimeIssues.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">executeTwoThreads<\/span>(){                                   <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string localString(<span style=\"color: #cc3300;\">\"local string\"<\/span>);         <em> <span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t1([localString]{\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Per Copy: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> localString <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    });\r\n    \r\n     std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t2([<span style=\"color: #555555;\">&amp;<\/span>localString]{\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Per Reference: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> localString <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    });\r\n    \r\n    t1.detach();                                          <em> <span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    t2.detach();                                           <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\r\n}\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>chrono_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    executeTwoThreads();\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>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><code>executeTwoThreads<\/code> (lines 1) starts two threads. Both threads are detached (lines 2 and 3) and print the local variable <code>localString<\/code> (line 4). The first thread captures the local variable by copy, and the second the local variable by reference. For simplicity reasons, I used a lambda expression in both cases to bind the arguments. Because the <code>executeTwoThreads<\/code> function doesn\u2019t wait until the two threads have finished, the thread<code> t2<\/code> refers to the local string, which is bound to the lifetime of the invoking function. This causes undefined behavior. Curiously, with GCC the maximum optimized executable<code> -O3<\/code> seems to work, and the non-optimized executable crashes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6558\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueLifetimeIssues.png\" alt=\"copiedValueLifetimeIssues\" width=\"600\" height=\"205\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueLifetimeIssues.png 934w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueLifetimeIssues-300x102.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/05\/copiedValueLifetimeIssues-768x262.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Thanks to thread-local storage, a thread can easily work on its data.<\/p>\n<h2>Thread-Specific Storage<\/h2>\n<p>Thread-specific or thread-local storage allows multiple threads to use local storage via a global access point. By using the storage specifier <code>thread_local<\/code>, a variable becomes a thread-local variable. This means you can use the thread-local variable without synchronization.<br \/>Assume you want to calculate the sum of all elements of a vector randValues. Doing it with a range-based for-loop is straightforward.<\/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;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> sum{};\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> n<span style=\"color: #555555;\">:<\/span> randValues) sum <span style=\"color: #555555;\">+=<\/span> n;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>But your PC has four cores. Therefore, you make a concurrent program out of the sequential program:<\/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;\">\/\/ threadLocallSummation.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;atomic&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;random&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> size <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10000000<\/span>;   \r\n\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> fir <span style=\"color: #555555;\">=<\/span>  <span style=\"color: #ff6600;\">2500000<\/span>;\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> sec <span style=\"color: #555555;\">=<\/span>  <span style=\"color: #ff6600;\">5000000<\/span>;\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> thi <span style=\"color: #555555;\">=<\/span>  <span style=\"color: #ff6600;\">7500000<\/span>;\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> fou <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10000000<\/span>;\r\n\r\nthread_local <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> tmpSum <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">sumUp<\/span>(std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span> sum, <span style=\"color: #006699; font-weight: bold;\">const<\/span> 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;&amp;<\/span> val, \r\n           <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> beg, <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> end) {\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">=<\/span> beg; i <span style=\"color: #555555;\">&lt;<\/span> end; <span style=\"color: #555555;\">++<\/span>i){\r\n        tmpSum <span style=\"color: #555555;\">+=<\/span> val[i];\r\n    }\r\n    sum.fetch_add(tmpSum);\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>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> randValues;\r\n  randValues.reserve(size);\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>mt19937 engine;\r\n  std<span style=\"color: #555555;\">::<\/span>uniform_int_distribution<span style=\"color: #555555;\">&lt;&gt;<\/span> uniformDist(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span>);\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> size; <span style=\"color: #555555;\">++<\/span>i) \r\n      randValues.push_back(uniformDist(engine));\r\n \r\n  std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span><span style=\"color: #555555;\">&gt;<\/span> sum{}; \r\n  \r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t1(sumUp, std<span style=\"color: #555555;\">::<\/span>ref(sum), std<span style=\"color: #555555;\">::<\/span>ref(randValues), <span style=\"color: #ff6600;\">0<\/span>, fir);\r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t2(sumUp, std<span style=\"color: #555555;\">::<\/span>ref(sum), std<span style=\"color: #555555;\">::<\/span>ref(randValues), fir, sec);\r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t3(sumUp, std<span style=\"color: #555555;\">::<\/span>ref(sum), std<span style=\"color: #555555;\">::<\/span>ref(randValues), sec, thi);\r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t4(sumUp, std<span style=\"color: #555555;\">::<\/span>ref(sum), std<span style=\"color: #555555;\">::<\/span>ref(randValues), thi, fou);   \r\n  \r\n  t1.join();\r\n  t2.join();\r\n  t3.join();\r\n  t4.join();\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Result: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum <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>You put the range-based for-loop into a function and let each thread calculate a fourth of the sum in the<code> thread_local<\/code> variable <code>tmpSum<\/code>. The line <code>sum.fetch_add(tmpSum)<\/code> (line 1) finally sums up all values in the atomic <code>sum<\/code>. You can read more about thread_local storage in my previous post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-local-data\">Thread-Local Data<\/a>&#8220;.<\/p>\n<p>Promises and futures share a protected data channel.<\/p>\n<h2>Future<\/h2>\n<p>C++11 provides futures and promise in three flavors:<code> std::async<\/code>,<code> std::packaged_tas<\/code>k, and the pair <code>std::promise<\/code> and<code> std::future<\/code>. The future is a read-only placeholder for the value that a promise sets. From the synchronization perspective, a promise\/future pair\u2019s critical property is that a protected data channel connects both. There are a few decisions to make when implementing a future.<\/p>\n<ul>\n<li>A future can ask for its value implicitly or explicitly with the <code>get<\/code> call, such as in C++.<\/li>\n<li>A future can eagerly or lazily start the computation. Only the promise <code>std::async<\/code> supports lazy evaluation via launch policies.<\/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: #006699; font-weight: bold;\">auto<\/span> lazyOrEager <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async([]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"LazyOrEager\"<\/span>; });\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> lazy <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async(std<span style=\"color: #555555;\">::<\/span>launch<span style=\"color: #555555;\">::<\/span>deferred, []{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Lazy\"<\/span>; });\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> eager <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async(std<span style=\"color: #555555;\">::<\/span>launch<span style=\"color: #555555;\">::<\/span>async, []{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Eager\"<\/span>; });<br \/>\r\nlazyOrEager.get();\r\nlazy.get();\r\neager.get();\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If I don\u2019t specify a launch policy, it\u2019s up to the system to start the job eager or lazy. Using the launch policy <code>std::launch::async<\/code>, a new thread is created, and the promise immediately starts its job. This<br \/>is in contrast to the launch policy<code> std::launch::deferre<\/code>d. The call <code>eager.get()<\/code> starts the promise. Additionally, the promise is executed in the thread requesting the result with <code>get<\/code>.<\/p>\n<p>If you want to read more about futures in C++, read the following post: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/asynchronous-function-calls\">Asynchronous Function Calls<\/a>&#8220;.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>&nbsp;No data race can happen if you don\u2019t write and read data concurrently. In my next, I will write about patterns that help you to protect against mutation.<\/p>\n<p>&nbsp;<\/p>\n<p>{module title=&#8221;Marketing&#8221;}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you don\u2019t share, no data races can happen. Not sharing means that your thread works on local variables. This can be achieved by copying the value, using thread-specific storage, or transferring the result of a thread to its associated future via a protected data channel.<\/p>\n","protected":false},"author":21,"featured_media":6554,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6559","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=6559"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6554"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}