{"id":4753,"date":"2016-05-15T06:32:00","date_gmt":"2016-05-15T06:32:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/thread-safe-initialization-of-data\/"},"modified":"2023-06-26T12:57:30","modified_gmt":"2023-06-26T12:57:30","slug":"thread-safe-initialization-of-data","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/thread-safe-initialization-of-data\/","title":{"rendered":"Thread-Safe Initialization of Data"},"content":{"rendered":"<p>The story is simple if the data is not modified when shared between threads. The data has only to be initialized in the thread-safe way. <strong>It is not necessary to use an expensive lock for each access.<\/strong><\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>There are three ways in C++ to initialize variables in a thread-safe way.<\/p>\n<ol>\n<li>Constant expressions<\/li>\n<li>The function<span style=\"font-family: courier new,courier;\"> std::call_once<\/span>, in combination with the flag <span style=\"font-family: courier new,courier;\">std::once_flag<\/span><\/li>\n<li>Static variables with block scope<\/li>\n<\/ol>\n<h2>Constant expressions<\/h2>\n<p>Constant expressions are expressions that the compiler can initialize during compile time. So, they are implicit thread-safe. Using the keyword <span style=\"font-family: courier new,courier;\">constexpr<\/span> in front of the expression type makes it a constant expression.<\/p>\n<p>constexpr <span style=\"color: #2b91af;\">double<\/span> pi=3.14;&nbsp;<\/p>\n<p>In addition, user-defined types can also be constant expressions. There are a few restrictions for those types to initialize them at compile time.<\/p>\n<ul>\n<li>They must not have virtual methods or a virtual base class.<\/li>\n<li>Their constructor must be empty, and itself be a constant expression.<\/li>\n<li>Their methods, which can be callable at compile time, must be constant expressions.<\/li>\n<\/ul>\n<p>My struct&nbsp;<span style=\"font-family: courier new,courier;\">MyDouble<\/span> satisfies all these requirements. So it&#8217;s possible to instantiate objects of <span style=\"font-family: courier new,courier;\">MyDouble<\/span> at compile time. This instantiation is thread-safe.<\/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<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">struct<\/span> MyDouble{\r\n  constexpr MyDouble(<span style=\"color: #2b91af;\">double<\/span> v): val(v){}\r\n  constexpr <span style=\"color: #2b91af;\">double<\/span> getValue(){ <span style=\"color: #0000ff;\">return<\/span> val; }\r\nprivate:\r\n  <span style=\"color: #2b91af;\">double<\/span> val;\r\n};\r\n\r\nconstexpr MyDouble myDouble(10.5);\r\nstd::cout &lt;&lt; myDouble.getValue() &lt;&lt; std::endl;\r\n<\/pre>\n<\/div>\n<h2>The function <span style=\"font-family: courier new,courier;\">call_once<\/span>, in combination with the <span style=\"font-family: courier new,courier;\">once_flag<\/span><\/h2>\n<p>By using the <span style=\"font-family: courier new,courier;\">std::call_once<\/span> function, you can register all callables. The<span style=\"font-family: courier new,courier;\"> std::once_flag<\/span> ensures that only one registered function will be invoked. So, you can register more different functions via the <span style=\"font-family: courier new,courier;\">once_flag<\/span>. Only one function is called.<\/p>\n<p>The short example shows the application of <span style=\"font-family: courier new,courier;\">std::call_once<\/span> and <span style=\"font-family: courier new,courier;\">std::once_flag.<\/span><\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ callOnce.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;mutex&gt;<\/span>\r\n \r\nstd::once_flag onceFlag;\r\n \r\n<span style=\"color: #2b91af;\">void<\/span> do_once(){\r\n  std::call_once(onceFlag, [](){ std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Only once.\"<\/span> &lt;&lt; std::endl; });\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(do_once);\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t2(do_once);\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t3(do_once);\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t4(do_once);\r\n \r\n  t1.join();\r\n  t2.join();\r\n  t3.join();\r\n  t4.join();\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 program starts in four threads (lines 17 &#8211; 20). Each of them should invoke the function <span style=\"font-family: courier new,courier;\">do_once<\/span>. The expected result is that the string &#8220;only once&#8221; is displayed only once.<\/p>\n<p><span style=\"color: #ff0000;\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4751\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/callOnce.png\" alt=\"callOnce\" width=\"568\" height=\"178\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/callOnce.png 568w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/callOnce-300x94.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><br \/><\/span><\/p>\n<p>The famous&nbsp;<a href=\"https:\/\/de.wikipedia.org\/wiki\/Singleton_(Entwurfsmuster)\">singleton pattern <\/a>guarantees only one instance of an object will be created. That is a challenging task in a multithreaded environment. But, thanks to&nbsp;<span style=\"font-family: courier new,courier;\">std:.call_once <\/span>and <span style=\"font-family: courier new,courier;\">std:once_flag<\/span> the job is a piece of cake. Now the singleton is initialized in a thread-safe way.&nbsp;<\/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\r\n38\r\n39\r\n40\r\n41<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ singletonCallOnce.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;mutex&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MySingleton<\/span>{\r\n\r\n  private:\r\n    <span style=\"color: #0000ff;\">static<\/span> std::once_flag initInstanceFlag;\r\n    <span style=\"color: #0000ff;\">static<\/span> MySingleton* instance;\r\n    MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\n    ~MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\n\r\n  public:\r\n    MySingleton(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n    MySingleton&amp; <span style=\"color: #0000ff;\">operator<\/span>=(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n\r\n    <span style=\"color: #0000ff;\">static<\/span> MySingleton* getInstance(){\r\n      std::call_once(initInstanceFlag,MySingleton::initSingleton);\r\n      <span style=\"color: #0000ff;\">return<\/span> instance;\r\n    }\r\n\r\n    <span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #2b91af;\">void<\/span> initSingleton(){\r\n      instance= <span style=\"color: #0000ff;\">new<\/span> MySingleton();\r\n    }\r\n};\r\n\r\nMySingleton* MySingleton::instance= nullptr;\r\nstd::once_flag MySingleton::initInstanceFlag;\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;\">\"MySingleton::getInstance(): \"<\/span>&lt;&lt; MySingleton::getInstance() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"MySingleton::getInstance(): \"<\/span>&lt;&lt; MySingleton::getInstance() &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>At first, the static<span style=\"font-family: courier new,courier;\"> std::once_fla<\/span>g. This is in line 9, declared and initialized in line 29. The static method<span style=\"font-family: courier new,courier;\"> getInstance<\/span> (lines 28 &#8211; 21) uses the flag to ensure that the static method<span style=\"font-family: courier new,courier;\"> initSingleton<\/span> (lines 23 &#8211; 25) is executed exactly once. In the body of the method, the singleton is created.<\/p>\n<p>The output of the program is not so thrilling. The <span style=\"font-family: courier new,courier;\">MySingleton::getIstance() <\/span>method displays the address of the singleton.<\/p>\n<p><span style=\"color: #ff0000;\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4752\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/singletonCallOnce.png\" alt=\"singletonCallOnce\" width=\"568\" height=\"190\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/singletonCallOnce.png 568w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/05\/singletonCallOnce-300x100.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><br \/><\/span><\/p>\n<p>The static story goes on.<\/p>\n<h2>Static variables with block scope<\/h2>\n<p>Static variables with block scope will be created precisely once. This characteristic is the base of the so-called Meyers Singleton, named after <a href=\"https:\/\/en.wikipedia.org\/wiki\/Scott_Meyers\">Scott Meyers.&nbsp;<\/a> This is by far the most elegant implementation of the singleton pattern.<\/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<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MySingleton<\/span>{\r\npublic:\r\n  <span style=\"color: #0000ff;\">static<\/span> MySingleton&amp; getInstance(){\r\n    <span style=\"color: #0000ff;\">static<\/span> MySingleton instance;\r\n    <span style=\"color: #0000ff;\">return<\/span> instance;\r\n  }\r\nprivate:\r\n  MySingleton();\r\n  ~MySingleton();\r\n  MySingleton(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n  MySingleton&amp; <span style=\"color: #0000ff;\">operator<\/span>=(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n\r\n};\r\n\r\nMySingleton::MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\nMySingleton::~MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  MySingleton::getInstance();\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Using the keyword <span style=\"font-family: courier new,courier;\">default,<\/span> you can request special methods from the compiler. They are special because only a compiler can create them. With <span style=\"font-family: courier new,courier;\">delete,<\/span> the result is that the automatically generated methods (constructor, for example) from the compiler will not be created and, therefore, can not be called. If you try to use them, you&#8217;ll get a compile-time error. What&#8217;s the point of the Meyers Singleton in multithreading programs? The Meyers Singleton is thread-safe.<\/p>\n<h2>A side note: Double-checked locking pattern<\/h2>\n<p>Wrong beliefs exist, that a double-checking locking pattern is an additional way for the thread-safe initialization of a singleton in a multithreading environment. The double-checked locking pattern is &#8211; in general &#8211; &nbsp;an unsafe way to initialize a singleton. It assumes guarantees in the classical implementation, which the Java, C#, or C++ memory model don&#8217;t give. The assumption is that the access of the singleton is atomic.<\/p>\n<p>But what is the double-checked locking pattern? The first idea to implement the singleton pattern in a thread-safe way is to protect the initialization of the singleton by a lock.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\">mutex myMutex;\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MySingleton<\/span>{\r\npublic:  \r\n  <span style=\"color: #0000ff;\">static<\/span> MySingleton&amp; getInstance(){    \r\n    lock_guard&lt;mutex&gt; myLock(myMutex);      \r\n    <span style=\"color: #0000ff;\">if<\/span>( !instance ) instance= <span style=\"color: #0000ff;\">new<\/span> MySingleton();    \r\n    <span style=\"color: #0000ff;\">return<\/span> *instance;  \r\n  }\r\nprivate:  \r\n  MySingleton();  \r\n  ~MySingleton();  \r\n  MySingleton(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;  \r\n  MySingleton&amp; <span style=\"color: #0000ff;\">operator<\/span>=(<span style=\"color: #0000ff;\">const<\/span> MySingleton&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n  <span style=\"color: #0000ff;\">static<\/span> MySingleton* instance;\r\n};\r\nMySingleton::MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\nMySingleton::~MySingleton()= <span style=\"color: #0000ff;\">default<\/span>;\r\nMySingleton* MySingleton::instance= nullptr;\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Any issues? Yes and no. The implementation is thread-safe. But there is a significant performance penalty. An expansive lock protects each singleton access in line 6. That also applies to reading access. Most time, it&#8217;s not necessary. Here comes the double-checked locking pattern to our rescue.<\/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\n2\r\n3\r\n4\r\n5\r\n6\r\n7<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">static<\/span> MySingleton&amp; getInstance(){    \r\n  <span style=\"color: #0000ff;\">if<\/span> ( !instance ){      \r\n    lock_guard&lt;mutex&gt; myLock(myMutex);      \r\n    <span style=\"color: #0000ff;\">if<\/span>( !instance ) instance= <span style=\"color: #0000ff;\">new<\/span> MySingleton();    \r\n  }<br \/><span style=\"color: #0000ff;\">&nbsp; r<span style=\"color: #0000ff;\">eturn<\/span> *instance;&nbsp;<\/span>\r\n<span style=\"color: #0000ff;\"><\/span>}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I use an inexpensive pointer comparison in line 2 instead of an expensive lock a. Only if I get a null pointer I apply the expensive lock on the singleton (line 3). Because there is the possibility that another thread will initialize the singleton between the pointer comparison (line 2) and the lock (line 3), I have to perform an additional pointer comparison on line 4. So the name is clear. Two times a check and one time a lock.<\/p>\n<p>Smart? Yes. Is thread safe? No.<\/p>\n<p>What is the problem? The call instance= new MySingleton() in line 4 consists of at least three steps.<\/p>\n<ol>\n<li>Allocate memory for <span style=\"font-family: courier new,courier;\">MySingleton<\/span><\/li>\n<li>Create the <span style=\"font-family: courier new,courier;\">MySingleton<\/span> object in the memory<\/li>\n<li>Let <span style=\"font-family: courier new,courier;\">instance<\/span> refer to the <span style=\"font-family: courier new,courier;\">MySingleton<\/span> object<\/li>\n<\/ol>\n<p>The problem: there is no guarantee about the sequence of these steps. For example, for optimization reasons, the processor can reorder the steps to the sequences 1,3, and 2. So, in the first step, the memory will be allocated; in the second step, the instance refers to an incomplete singleton. If, at that time, another thread tries to access the singleton, it compares the pointer and gets the answer <span style=\"font-family: courier new,courier;\">true<\/span>. So, the other thread has the illusion that it&#8217;s dealing with a complete singleton.<\/p>\n<p>The consequence is simple: program behavior is undefined.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>At first, I should continue in the<a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-local-data\"> next post<\/a> with the singleton pattern. But to write about the singleton pattern, you should have a basic knowledge of the memory model. So I continue in the sequence of my German blog. The next post will be about-thread local storage. If we are done with the high-end API of multithreading in C++, I&#8217;ll go further with the low-end API. <strong>(Proofreader Alexey Elymanov)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The story is simple if the data is not modified when shared between threads. The data has only to be initialized in the thread-safe way. It is not necessary to use an expensive lock for each access.<\/p>\n","protected":false},"author":21,"featured_media":4751,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[428,404,520],"class_list":["post-4753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading","tag-constexpr","tag-singleton","tag-static"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4753","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=4753"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4753\/revisions"}],"predecessor-version":[{"id":6981,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4753\/revisions\/6981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4751"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}