Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jdsherbert/threadsafe-singleton-class
C++ Threadsafe Singleton class using mutex and threadlock when accessing instance.
https://github.com/jdsherbert/threadsafe-singleton-class
boilerplate boilerplate-template cpp cpp11 cpp14 cpp17 cpp20 multithreading mutex mutex-lock mutex-locks mutex-synchronisation reference-implementation singleton singleton-pattern singletons
Last synced: about 1 month ago
JSON representation
C++ Threadsafe Singleton class using mutex and threadlock when accessing instance.
- Host: GitHub
- URL: https://github.com/jdsherbert/threadsafe-singleton-class
- Owner: JDSherbert
- License: mit
- Created: 2023-11-29T13:56:37.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-12-04T17:06:31.000Z (12 months ago)
- Last Synced: 2024-10-12T16:02:53.060Z (about 1 month ago)
- Topics: boilerplate, boilerplate-template, cpp, cpp11, cpp14, cpp17, cpp20, multithreading, mutex, mutex-lock, mutex-locks, mutex-synchronisation, reference-implementation, singleton, singleton-pattern, singletons
- Language: C++
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
![image](https://github.com/JDSherbert/Threadsafe-Singleton-Class/assets/43964243/8a288a74-0a1e-4fcb-a5d8-608bb7c9d46b)
# Threadsafe Singleton Class
-----------------------------------------------------------------------
-----------------------------------------------------------------------
## OverviewCreating a thread-safe singleton in C++ requires handling the potential race conditions that can occur when multiple threads attempt to access the singleton instance concurrently. One common approach is to use the double-checked locking pattern.
This example uses the mutex functions from the mutex library.Please note that in modern C++, you can use C++11's `std::call_once` with a `std::once_flag` instead of manually managing a mutex. This is generally considered a safer and more convenient option. However, the above example demonstrates the basic concept of thread safety in a singleton.
-----------------------------------------------------------------------