{"id":4755,"date":"2016-05-16T15:01:35","date_gmt":"2016-05-16T15:01:35","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/thread-local-data\/"},"modified":"2023-06-26T12:57:12","modified_gmt":"2023-06-26T12:57:12","slug":"thread-local-data","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/thread-local-data\/","title":{"rendered":"Thread-Local Data"},"content":{"rendered":"<p>By using the keyword <span style=\"font-family: courier new,courier;\">thread_local<\/span>, you define the thread local data. Thread-local can&nbsp;easily be explained in a few words.<\/p>\n<p><!--more--><\/p>\n<h2><span style=\"font-family: courier new,courier;\">thread_local<\/span><\/h2>\n<p>When needed, thread local data will be created for each thread. thread-local data exclusively belongs to the thread and behaves like static data. That means it will be created at its first usage, and its lifetime is bound to the thread&#8217;s lifetime. Often thread local data is called thread local storage.<\/p>\n<\/p>\n<p>Dealing with thread local data is straightforward.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ threadLocal.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&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\nthread_local std::string s(<span style=\"color: #a31515;\">\"hello from \"<\/span>);\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> addThreadLocal(std::string <span style=\"color: #0000ff;\">const<\/span>&amp; s2){\r\n\r\n  s+=s2;\r\n  <span style=\"color: #008000;\">\/\/ protect std::cout<\/span>\r\n  std::lock_guard&lt;std::mutex&gt; guard(coutMutex);\r\n  std::cout &lt;&lt; s &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"&amp;s: \"<\/span> &lt;&lt; &amp;s &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; std::endl;\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::<span style=\"color: #0000ff;\">thread<\/span> t1(addThreadLocal,<span style=\"color: #a31515;\">\"t1\"<\/span>); \r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t2(addThreadLocal,<span style=\"color: #a31515;\">\"t2\"<\/span>); \r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t3(addThreadLocal,<span style=\"color: #a31515;\">\"t3\"<\/span>); \r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t4(addThreadLocal,<span style=\"color: #a31515;\">\"t4\"<\/span>); \r\n\r\n  t1.join();\r\n  t2.join();\r\n  t3.join();\r\n  t4.join();\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By using the keyword <span style=\"font-family: courier new,courier;\">thread_local<\/span> in line 8, the thread-local string <span style=\"font-family: courier new,courier;\">s<\/span> is created. The threads <span style=\"font-family: courier new,courier;\">t1 &#8211; t4<\/span> (lines 27 &#8211; 30) use the function <span style=\"font-family: courier new,courier;\">addThreadLocal<\/span> (lines 12 &#8211; 21) as their work package. The threads get as argument the strings &#8220;t1&#8221; to &#8220;t4&#8221; respectively and add them to the tread-local string <span style=\"font-family: courier new,courier;\">s<\/span>. In addition, <span style=\"font-family: courier new,courier;\">addThreadLocal<\/span> displays the address of the string <span style=\"font-family: courier new,courier;\">s<\/span> in line 18.<br \/>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4754\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/threadLocal.png\" alt=\"threadLocal\" width=\"527\" height=\"335\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/threadLocal.png 527w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/threadLocal-300x191.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/p>\n<p>The program&#8217;s output shows it implicitly in line 17&nbsp;and explicitly by the address in line 18. The tread-local string is created for each string <span style=\"font-family: courier new,courier;\">s<\/span>. First, each output shows a new thread-local string; second, each string <span style=\"font-family: courier new,courier;\">s <\/span>has a different address.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Condition variables are easy to use wrong. Why? Have a look at the<a href=\"https:\/\/www.modernescpp.com\/index.php\/condition-variables\"> next post<\/a>. (<strong>Proofreader Arne Mertz<\/strong>)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By using the keyword thread_local, you define the thread local data. Thread-local can&nbsp;easily be explained in a few words.<\/p>\n","protected":false},"author":21,"featured_media":4754,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[487],"class_list":["post-4755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading","tag-thread_local"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4755","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=4755"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4755\/revisions"}],"predecessor-version":[{"id":6980,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4755\/revisions\/6980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4754"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}