From 898d08f568b6a8f7c22cb8eedc2ce9199b540066 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 7 Mar 2016 22:44:43 +0300 Subject: std::call_once: wrap text at column 79 --- ...std-call-once-bug-in-visual-studio-2012-2013.md | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to '_posts/2015-07-03-std-call-once-bug-in-visual-studio-2012-2013.md') 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 %}
-

-The point is that the Logger::get_instance routine above wasn't +

The point is that the Logger::get_instance routine above wasn't thread-safe until C++11. -Imagine what might happen if Loggers constructor takes some time to -initialize the instance. +Imagine what might happen if Loggers constructor takes some time +to initialize the instance. If a couple of threads then call get_instance, 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.

-

-Since C++11 includes the proposal for +

Since C++11 includes the proposal for "Dynamic Initialization and Destruction with Concurrency" 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 std::call_once, which seemed to implement exactly what I -wanted. -

+wanted.

## 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. -- cgit v1.2.3