{"id":3916,"date":"2014-03-30T06:23:37","date_gmt":"2014-03-30T06:23:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-run-time-costs-are-there-to-initializing-a-static-with-a-variable-value-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:23:37","modified_gmt":"2014-03-30T06:23:37","slug":"what-run-time-costs-are-there-to-initializing-a-static-with-a-variable-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-run-time-costs-are-there-to-initializing-a-static-with-a-variable-value-collection-of-common-programming-errors\/","title":{"rendered":"What run-time costs are there to initializing a static with a variable value?-Collection of common programming errors"},"content":{"rendered":"<p>Initialisation from a compile-time constant (alternate 2) will most likely happen during program startup, with no cost each time the function is called.<\/p>\n<p>Thread-safe initialisation of a local static variable will cause the compiler to generate something like this pseudocode:<\/p>\n<pre><code>static bool alternate1;\n\nstatic bool __initialised = false;\nstatic __lock_type __lock;\nif (!__initialised) {\n    acquire(__lock);\n    if (!__initialised) {\n        alternate1 = value;\n        __initialised = true;\n    }\n    release(__lock);\n}\n<\/code><\/pre>\n<p>So there is likely to be a test of a flag each time the function is called (perhaps involving a memory barrier or other synchronisation primitive), and a further cost of acquiring and releasing a lock the first time.<\/p>\n<p>Note that in your code, <code>foo()<\/code> is called every time, whether or not the variable is initialised yet. It would only be called the first time, if you changed the initialisation to<\/p>\n<pre><code>static bool alternate1 = foo();\n<\/code><\/pre>\n<p>The details are implementation-dependent of course; this is based on my observations of the code produced by GCC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Initialisation from a compile-time constant (alternate 2) will most likely happen during program startup, with no cost each time the function is called. Thread-safe initialisation of a local static variable will cause the compiler to generate something like this pseudocode: static bool alternate1; static bool __initialised = false; static __lock_type __lock; if (!__initialised) { acquire(__lock); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3916","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3916"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3916\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}