https://github.com/bardifarsi/lazyinitialization
This C++20 solution enables lazy initialization for multithreaded tasks. It efficiently initializes an expensive object only when needed, ensuring thread safety. It utilizes std::once_flag and std::atomic<bool> for synchronization, optimizing resource utilization and scalability.
https://github.com/bardifarsi/lazyinitialization
concurrency concurrency-patterns concurrent-programming cpp cpp20 lazy-init lazy-initialization lazyinitializationexception multithreading object-oriented-programming oop parallel-computing parallel-processing parallel-programming singleton singleton-design-pattern singleton-pattern singletons
Last synced: 3 months ago
JSON representation
This C++20 solution enables lazy initialization for multithreaded tasks. It efficiently initializes an expensive object only when needed, ensuring thread safety. It utilizes std::once_flag and std::atomic<bool> for synchronization, optimizing resource utilization and scalability.
- Host: GitHub
- URL: https://github.com/bardifarsi/lazyinitialization
- Owner: BardiFarsi
- Created: 2024-04-06T20:32:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-06T23:33:00.000Z (about 1 year ago)
- Last Synced: 2024-12-29T17:43:01.500Z (5 months ago)
- Topics: concurrency, concurrency-patterns, concurrent-programming, cpp, cpp20, lazy-init, lazy-initialization, lazyinitializationexception, multithreading, object-oriented-programming, oop, parallel-computing, parallel-processing, parallel-programming, singleton, singleton-design-pattern, singleton-pattern, singletons
- Language: C++
- Homepage:
- Size: 229 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lazy Initialization with Multithreading in C++
## Overview
This repository contains a C++ solution that demonstrates the proper way of implementing lazy initialization with multithreading. Lazy initialization is a design pattern used to defer the creation of an object until it is needed. Multithreading is utilized to ensure thread safety and efficient utilization of system resources.## Requirements
- C++20 compatible compiler
- This code has been tested and confirmed to work with both Mingw g++ and Windows MSVC compilers. GCC compiler should be avoided.
- Standard C++ libraries
- Git## Usage
To use this solution, follow these steps:
1. Clone the repository to your local machine:
```bash
git clone https://github.com/BardiParsi/LazyInitialization.git
```
2. Navigate to the cloned directory:
```bash
cd LazyInitialization1
```
3. Compile the source code:
```bash
g++ -std=c++20 -pthread main.cpp -o main
```
4. Run the executable:
```bash
./main
```## Implementation Details
The solution consists of the following key components:
- `ExpensiveObject`: Represents an expensive object that is initialized lazily.
- `LazyInitialization`: Manages the lazy initialization of `ExpensiveObject` using a mutex and atomic flags to ensure thread safety and efficient resource utilization.
- `main`: Demonstrates the usage of `LazyInitialization` with multiple threads.## Code Explanation
- `ExpensiveObject`: Simulates an expensive object that takes some time to initialize.
- `LazyInitialization`: Implements lazy initialization using `std::once_flag` and `std::atomic`. The `useObject()` function ensures that the object is initialized only once and is thread-safe.
- `main`: Creates a vector of multiple threads that attempt to use the `ExpensiveObject` simultaneously, demonstrating the effectiveness of lazy initialization and multithreading.## Contributing
Contributions are welcome! Feel free to submit issues or pull requests for any improvements or additional features.## License
This project is licensed under the GNU General Public License (GPL).
Author: Bardi (Masoud) Parsi
Contact: [email protected]