aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/_posts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md29
1 files changed, 14 insertions, 15 deletions
diff --git a/_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md b/_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md
index 68c5f95..728b74f 100644
--- a/_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md
+++ b/_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md
@@ -14,7 +14,8 @@ Library implementation shipped with Microsoft Visual Studio 2012/2013.
### Licensing
-This post, including code samples, is licenced under the terms of the MIT License.
+This post, including code samples, is licenced under the terms of the MIT
+License.
See
[LICENSE.txt](https://github.com/egor-tensin/cpp_tips/blob/gh-pages/LICENSE.txt)
for details.
@@ -100,10 +101,11 @@ private:
Note that this was at the time when the
[N2660](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm)
-standard proposal wasn't implemented in the compilers shipped with Visual Studio
-2012/2013.
-If it was, I wouldn't, of course, need to employ this `std::call_once` trickery,
-and the implementation would be much simpler, i.e. something like this:
+standard proposal wasn't implemented in the compilers shipped with Visual
+Studio 2012/2013.
+If it was, I wouldn't, of course, need to employ this `std::call_once`
+trickery, and the implementation would be much simpler, i.e. something like
+this:
{% highlight c++ %}
class Logger
@@ -122,34 +124,31 @@ private:
{% endhighlight %}
<div class="alert alert-info">
-<p>
-The point is that the <code>Logger::get_instance</code> routine above wasn't
+<p>The point is that the <code>Logger::get_instance</code> routine above wasn't
thread-safe until C++11.
-Imagine what might happen if <code>Logger</code>s constructor takes some time to
-initialize the instance.
+Imagine what might happen if <code>Logger</code>s constructor takes some time
+to initialize the instance.
If a couple of threads then call <code>get_instance</code>, the first thread
might begin the initialization process, making the other thread believe that it
doesn't need to initialize the instance anymore (goodbye, C++03).
The second thread might then return a reference to a not fully initialized (and
hence, unsafe to use) instance.</p>
-<p>
-Since C++11 includes the proposal for
+<p>Since C++11 includes the proposal for
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm">"Dynamic Initialization and Destruction with Concurrency"</a>
mentioned above, this routine would indeed be thread-safe in C++11.
Unfortunately, the compilers shipped with Visual Studio 2012/2013 didn't
implement this particular proposal, which caused me to turn my eyes to
<code>std::call_once</code>, which seemed to implement exactly what I
-wanted.
-</p>
+wanted.</p>
</div>
## The bug
Unfortunately, matters became a bit more complicated when I tried to have two
singleton classes.
-I had `Logger`, like in the example above, and some kind of a "master" singleton
-(let's call it `Duke`).
+I had `Logger`, like in the example above, and some kind of a "master"
+singleton (let's call it `Duke`).
These two classes both inherited from `Singleton`, which I thought was nice.
`Duke`s constructor was heavy and complicated and definetely required some
logging to be done.