Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        

![image](https://github.com/JDSherbert/Threadsafe-Singleton-Class/assets/43964243/8a288a74-0a1e-4fcb-a5d8-608bb7c9d46b)

# Threadsafe Singleton Class


Stars Badge
Forks Badge
Watchers Badge
Issues Badge

-----------------------------------------------------------------------


C++ Template



License



-----------------------------------------------------------------------
## Overview

Creating 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.

-----------------------------------------------------------------------