{"id":4741,"date":"2016-04-30T08:24:39","date_gmt":"2016-04-30T08:24:39","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/threads-sharing-data\/"},"modified":"2016-04-30T08:24:39","modified_gmt":"2016-04-30T08:24:39","slug":"threads-sharing-data","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/threads-sharing-data\/","title":{"rendered":"Threads Sharing Data"},"content":{"rendered":"<p>One of the biggest challenges of thread management begins when the threads share non-const data<\/p>\n<p><!--more--><\/p>\n<h2>Data race and critical section<\/h2>\n<p>In threads using shared data, you often hear the expressions race condition and critical section. But what&#8217;s that?&nbsp;<\/p>\n<dl>\n<dt><a id=\"RaceCondition\">Data Race<\/a><\/dt>\n<dd>&nbsp;&nbsp;&nbsp; A data race is a state, in which at least two threads access shared data at the same time, and at least one of the threads is a writer.<\/dd>\n<dt><a id=\"CriticalSection\">Critical Section<\/a><\/dt>\n<dd>&nbsp;&nbsp;&nbsp; A critical section is a section of the code, which not more than one thread should access at any point in time.<\/dd>\n<dd><\/dd>\n<dd>\n<p>&nbsp;<\/p>\n<p>In case the program has a race condition, the program behavior is undefined. To say it differently, anything can happen.<\/p>\n<p>A nice way to visualize a race condition is to let a few threads write to <span style=\"font-family: courier new,courier;\">std::cout<\/span>. <span style=\"font-family: courier new,courier;\">std::cout<\/span> is the shared object (output stream), that should be protected from simultaneous access by multiple threads.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ coutUnsynchronized.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Worker<\/span>{\r\npublic:\r\n  Worker(std::string n):name(n){};\r\n  \r\n    <span style=\"color: #2b91af;\">void<\/span> operator() (){\r\n      <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #2b91af;\">int<\/span> i= 1; i &lt;= 3; ++i){\r\n\t<span style=\"color: #008000;\">\/\/ begin work<\/span>\r\n\tstd::this_thread::sleep_for(std::chrono::milliseconds(200));\r\n\t<span style=\"color: #008000;\">\/\/ end work<\/span>\r\n\tstd::cout &lt;&lt; name &lt;&lt; <span style=\"color: #a31515;\">\": \"<\/span> &lt;&lt; <span style=\"color: #a31515;\">\"Work \"<\/span> &lt;&lt; i &lt;&lt; <span style=\"color: #a31515;\">\" done !!!\"<\/span> &lt;&lt; std::endl;\r\n      }\r\n      \r\n    }\r\nprivate:\r\n  std::string name;\r\n};\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Boss: Let's start working.\\n\\n\"<\/span>;\r\n \r\n  std::<span style=\"color: #0000ff;\">thread<\/span> herb= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"Herb\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> andrei= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"  Andrei\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> scott= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"    Scott\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> bjarne= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"      Bjarne\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> andrew= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"        Andrew\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> david= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"          David\"<\/span>));\r\n  \r\n  herb.join();\r\n  andrei.join();\r\n  scott.join();\r\n  bjarne.join();\r\n  andrew.join();\r\n  david.join();\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\"<\/span> &lt;&lt; <span style=\"color: #a31515;\">\"Boss: Let's go home.\"<\/span> &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The boss assigns three work packages (lines 11 &#8211; 17) to each of its six workers (lines 32 &#8211; 36). When a worker is done with their work package it screams out loudly to the boss (line 16). When the boss has gotten notifications from all workers, it sends them home (line 45).<span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p>&nbsp;<strong>What a mess!<\/strong><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4738\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker.png\" alt=\"bossWorker\" width=\"797\" height=\"502\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker.png 797w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker-300x189.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker-768x484.png 768w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<p>The same mess the next day. The workers scream out loudly. Totally unsynchronized.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4739\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker1.png\" alt=\"bossWorker1\" width=\"797\" height=\"502\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker1.png 797w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker1-300x189.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorker1-768x484.png 768w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<\/dd>\n<dd><\/dd>\n<dd><\/dd>\n<dd><\/dd>\n<dd><\/dd>\n<dd><\/dd>\n<dd><\/dd>\n<dd>\n<p>The first solution is a mutex. A mutex ensures, that each thread exclusively accesses the shared variable <span style=\"font-family: courier new,courier;\">std::cout<\/span>. <span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<h3><span style=\"font-family: arial,helvetica,sans-serif; color: #000000;\">A side note: <span style=\"font-family: courier new,courier;\">std::cout<\/span> is thread-safe<br \/><\/span><\/h3>\n<\/dd>\n<dd>The C++11 standard guarantees that you must not protect the single characters written to <span style=\"font-family: courier new,courier;\">std::cout.<\/span> Each character will atomically be written. Of course, it is possible that more output statements, like in the example, will interleave. But that is only an <em>optical<\/em> issue. The program is well-defined. The remark is valid for all input and output streams.<\/dd>\n<dd><\/dd>\n<dd>\n<h2>Mutex<\/h2>\n<p>Mutex stands for <strong>mut<\/strong>ual<strong> ex<\/strong>clusion. It ensures, that only one thread can access a critical section.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ coutSynchronized.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;mutex&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n\r\nstd::mutex coutMutex;\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Worker<\/span>{\r\npublic:\r\n  Worker(std::string n):name(n){};\r\n \r\n    <span style=\"color: #2b91af;\">void<\/span> operator() (){\r\n      <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #2b91af;\">int<\/span> i= 1; i &lt;= 3; ++i){\r\n\t<span style=\"color: #008000;\">\/\/ begin work<\/span>\r\n\tstd::this_thread::sleep_for(std::chrono::milliseconds(200));\r\n\t<span style=\"color: #008000;\">\/\/ end work<\/span>\r\n\tcoutMutex.lock();\r\n\tstd::cout &lt;&lt; name &lt;&lt; <span style=\"color: #a31515;\">\": \"<\/span> &lt;&lt; <span style=\"color: #a31515;\">\"Work \"<\/span> &lt;&lt; i &lt;&lt; <span style=\"color: #a31515;\">\" done !!!\"<\/span> &lt;&lt; std::endl;\r\n\tcoutMutex.unlock();\r\n      }\r\n    }\r\nprivate:\r\n  std::string name;\r\n};\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Boss: Let's start working.\"<\/span> &lt;&lt; <span style=\"color: #a31515;\">\"\\n\\n\"<\/span>;\r\n \r\n  std::<span style=\"color: #0000ff;\">thread<\/span> herb= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"Herb\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> andrei= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"  Andrei\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> scott= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"    Scott\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> bjarne= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"      Bjarne\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> andrew= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"        Andrew\"<\/span>));\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> david= std::<span style=\"color: #0000ff;\">thread<\/span>(Worker(<span style=\"color: #a31515;\">\"          David\"<\/span>));\r\n  \r\n  herb.join();\r\n  andrei.join();\r\n  scott.join();\r\n  bjarne.join();\r\n  andrew.join();\r\n  david.join();\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\"<\/span> &lt;&lt; <span style=\"color: #a31515;\">\"Boss: Let's go home.\"<\/span> &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The key difference to the first example is lines 19 to 21. By invoking the methods <span style=\"font-family: courier new,courier;\">coutMutex.lock()<\/span> and <span style=\"font-family: courier new,courier;\">coutMutex.unlock()<\/span>; you define the exclusive section. This section can only be accessed by, at most, a single thread. The access to <span style=\"font-family: courier new,courier;\">std::cout<\/span> is synchronized, and the mess becomes harmonious.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4740\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorkerSynchonized.png\" alt=\"bossWorkerSynchonized\" width=\"567\" height=\"498\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorkerSynchonized.png 567w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/bossWorkerSynchonized-300x263.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Mutexes have a lot of issues, which I will discuss in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-risk-of-mutexes\">next pos<\/a>t. (<strong>Proofreader Alexey<\/strong> <strong>Elymanov<\/strong>)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<span id=\"transmark\"> <br \/><\/span><\/p>\n<\/dd>\n<dd><\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>One of the biggest challenges of thread management begins when the threads share non-const data<\/p>\n","protected":false},"author":21,"featured_media":4738,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[],"class_list":["post-4741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4741","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=4741"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4738"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}