Linked Questions

251 votes
2 answers
100k views

I know this is an often asked question, but as there are so many variants, I'd like to re-state it, and hopefully have an answer reflecting the current state. Something like Logger& g_logger() { ...
Ralph Zhang's user avatar
  • 5,443
35 votes
4 answers
55k views

I've seen implementations of Singleton patterns where instance variable was declared as static variable in GetInstance method. Like this: SomeBaseClass &SomeClass::GetInstance() { static ...
okutane's user avatar
  • 14.4k
1 vote
0 answers
316 views

I have a parallel region, I have a static variable defined inside this region, like this #pragma omp parallel { static int var; // some stuff } Without threadprivate, static variables are ...
Kaiyakha's user avatar
  • 2,075
0 votes
1 answer
196 views

This question was closed as duplicate and hence making the question more explicit and reopening the question: The questions are : 1) Is the implementation below thread safe or not? 2) What are the ...
mukeshkumar's user avatar
  • 2,788
0 votes
1 answer
86 views

I've seen posts about this issue but I'm still trying to figure it out. Is this way alright for implementing a safe singelton? I'm using mutex, static member and return its reference. #include <...
Maor Cohen's user avatar
1 vote
0 answers
57 views

When class B is used as a static local variable in class A constructor, does c++11 will make sure func() be initialized only once in a thread-safe manner? Are the codes commented out redundant? void ...
Yulong Ao's user avatar
  • 1,263
2 votes
0 answers
30 views

I encountered a behavior I didn't understand. Here is test code of a common singleton implementation, Code from the link: #include <iostream> using namespace std; class CFoo { public: ...
Tomer W's user avatar
  • 3,413
920 votes
24 answers
1.0m views

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real-life example): // a lot of methods are omitted ...
Artem Barger's user avatar
  • 41.3k
66 votes
2 answers
73k views

I have been reading a lot about Singletons, when they should and shouldn't be used, and how to implement them safely. I am writing in C++11, and have come across the Meyer's lazy initialized ...
lbrendanl's user avatar
  • 2,704
47 votes
3 answers
17k views

In the following program I attempt the make the print function thread-safe by using a function-local mutex object: #include <iostream> #include <chrono> #include <mutex> #include &...
StackedCrooked's user avatar
24 votes
3 answers
3k views

Could someone explain this statement: shared variables x = 0, y = 0 Core 1 Core 2 x = 1; y = 1; r1 = y; r2 = x; How is it possible to have r1 == 0 and r2 == 0 on x86 processors? ...
Ation's user avatar
  • 731
6 votes
4 answers
12k views

I have a singleton class with a private constructor. In the static factory method I do the following: shared_ptr<MyClass> MyClass::GetInstance() { static once_flag onceFlag; call_once(...
user2683038's user avatar
3 votes
2 answers
3k views

I came up with this while trying to create a singleton. Example: ( I am trying to make MySelf a singleton which is thread safe and without using double checked locking ) class MySelf { private: ...
Deamonpog's user avatar
  • 815
0 votes
2 answers
2k views

I am writting a thread safe singleton class as follows. The following implementation ensures that only one instance of the class in created. My use case is that I am using this instance in a multi ...
danish sodhi's user avatar
  • 1,963
0 votes
6 answers
5k views

The static method GetUI is called with mouse events, however noticed from debug that the constructor is called twice in some very rare cases with rush of mouse events. The question is it the ...
Michael Bishara's user avatar

15 30 50 per page
1
2 3 4 5