From cbb45d7c46dd3ef4060f16cc3d62dc37fd759909 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 30 May 2017 06:48:51 +0300 Subject: import std_call_once_bug/ from gh-pages I rewrote this repository's history completely in an attempt to organize things properly. One of those things is moving code to master. I couldn't figure out how to preserve the history though. Luckily, it's a very small code sample. --- std_call_once_bug/CMakeLists.txt | 5 ++ std_call_once_bug/README.md | 27 ++++++++ std_call_once_bug/sample.cpp | 131 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 std_call_once_bug/CMakeLists.txt create mode 100644 std_call_once_bug/README.md create mode 100644 std_call_once_bug/sample.cpp (limited to 'std_call_once_bug') diff --git a/std_call_once_bug/CMakeLists.txt b/std_call_once_bug/CMakeLists.txt new file mode 100644 index 0000000..31d45de --- /dev/null +++ b/std_call_once_bug/CMakeLists.txt @@ -0,0 +1,5 @@ +project(std_call_once_bug CXX) + +include(../common.cmake) + +add_executable(sample sample.cpp) diff --git a/std_call_once_bug/README.md b/std_call_once_bug/README.md new file mode 100644 index 0000000..c42f191 --- /dev/null +++ b/std_call_once_bug/README.md @@ -0,0 +1,27 @@ +std::call_once bug in Visual C++ 2012/2013 +========================================== + +Code samples from the post "std::call_once bug in Visual C++ 2012/2013". + +Building +-------- + +Create the build files using CMake and build using your native build tools +(Visual Studio/make/etc.). + +In the example below, the project directory is +"C:\workspace\personal\cpp-notes" and Visual Studio 2013 is used, targeting +x86. + + > cmake -G "Visual Studio 12 2013" C:\workspace\personal\cpp-notes\std_call_once_bug + ... + + > cmake --build . --config release + ... + +See also +-------- + +* [License] + +[License]: ../README.md#license diff --git a/std_call_once_bug/sample.cpp b/std_call_once_bug/sample.cpp new file mode 100644 index 0000000..22a63c6 --- /dev/null +++ b/std_call_once_bug/sample.cpp @@ -0,0 +1,131 @@ +// Copyright (c) 2016 Egor Tensin +// This file is part of the "C++ notes" project. +// For details, see https://github.com/egor-tensin/cpp-notes. +// Distributed under the MIT License. + +#include + +#include +#include +#include +#include +#include +#include + +namespace +{ + template + class Singleton + { + public: + static Derived& get_instance() + { + std::call_once(initialized_flag, &initialize_instance); + return Derived::get_instance_unsafe(); + } + + protected: + Singleton() = default; + ~Singleton() = default; + + static Derived& get_instance_unsafe() + { + static Derived instance; + return instance; + } + + private: + static void initialize_instance() + { + Derived::get_instance_unsafe(); + } + + static std::once_flag initialized_flag; + + Singleton(const Singleton&) = delete; + Singleton& operator=(const Singleton&) = delete; + }; + + template + std::once_flag Singleton::initialized_flag; + + class Logger : public Singleton + { + public: + Logger& operator<<(const char*) + { + return *this; + } + + private: + Logger() + { + std::this_thread::sleep_for(std::chrono::seconds{3}); + } + + ~Logger() = default; + + friend class Singleton; + }; + + class Duke : public Singleton + { + private: + Duke() + { + Logger::get_instance() << "started Duke's initialization"; + std::this_thread::sleep_for(std::chrono::seconds{10}); + Logger::get_instance() << "finishing Duke's initialization"; + } + + ~Duke() = default; + + friend class Singleton; + }; + + std::mutex timestamp_mtx; + + std::string get_timestamp() + { + std::lock_guard lck{timestamp_mtx}; + const auto tt = std::time(NULL); + return std::ctime(&tt); + } + + void entered(const char* f) + { + std::ostringstream oss; + oss << "Entered " << f << " at " << get_timestamp(); + std::cout << oss.str(); + } + + void exiting(const char* f) + { + std::ostringstream oss; + oss << "Exiting " << f << " at " << get_timestamp(); + std::cout << oss.str(); + } + + void get_logger() + { + entered(__FUNCTION__); + Logger::get_instance(); + exiting(__FUNCTION__); + } + + void get_duke() + { + entered(__FUNCTION__); + Duke::get_instance(); + exiting(__FUNCTION__); + } +} + +int main() +{ + std::thread t1{&get_duke}; + std::thread t2{&get_logger}; + t1.join(); + t2.join(); + return 0; +} -- cgit v1.2.3