aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/std_call_once_bug/sample.cpp
blob: 01b5092dbf3e1340f62afa61da344b523f841ebb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "Egor's blog" project.
// For details, see https://github.com/egor-tensin/blog.
// Distributed under the MIT License.

#include <ctime>

#include <chrono>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>

namespace
{
    template <typename Derived>
    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 <typename Derived>
    std::once_flag Singleton<Derived>::initialized_flag;

    class Logger : public Singleton<Logger>
    {
    public:
        Logger& operator<<(const char*)
        {
            return *this;
        }

    private:
        Logger()
        {
            std::this_thread::sleep_for(std::chrono::seconds{3});
        }

        ~Logger() = default;

        friend class Singleton<Logger>;
    };

    class Duke : public Singleton<Duke>
    {
    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<Duke>;
    };

    std::mutex timestamp_mtx;

    std::string get_timestamp()
    {
        std::lock_guard<std::mutex> 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;
}