Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased multithreading security #463

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions cpputil/singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,23 @@
private: \
DISABLE_COPY(Class) \
static Class* s_pInstance; \
static std::once_flag s_initFlag; \
static std::mutex s_mutex;

#define SINGLETON_IMPL(Class) \
Class* Class::s_pInstance = NULL; \
std::once_flag Class::s_initFlag; \
std::mutex Class::s_mutex; \
Class* Class::instance() { \
if (s_pInstance == NULL) { \
s_mutex.lock(); \
if (s_pInstance == NULL) { \
s_pInstance = new Class; \
} \
s_mutex.unlock(); \
} \
return s_pInstance; \
std::call_once(s_initFlag, []() {s_pInstance = new Class;}); \
return s_pInstance; \
} \
void Class::exitInstance() { \
s_mutex.lock(); \
if (s_pInstance) { \
std::lock_guard<std::mutex> lock(s_mutex); \
if (s_pInstance) { \
delete s_pInstance; \
s_pInstance = NULL; \
} \
s_mutex.unlock(); \
s_pInstance = nullptr; \
} \
}

#endif // HV_SINGLETON_H_