{"id":23679433,"url":"https://github.com/bardifarsi/lazyinitialization","last_synced_at":"2025-08-31T11:40:03.243Z","repository":{"id":231927615,"uuid":"783058671","full_name":"BardiFarsi/LazyInitialization","owner":"BardiFarsi","description":"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\u003cbool\u003e for synchronization, optimizing resource utilization and scalability.","archived":false,"fork":false,"pushed_at":"2024-04-06T23:33:00.000Z","size":234,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T17:43:01.500Z","etag":null,"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"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BardiFarsi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-06T20:32:04.000Z","updated_at":"2024-04-11T23:24:53.000Z","dependencies_parsed_at":"2024-11-11T20:00:21.328Z","dependency_job_id":null,"html_url":"https://github.com/BardiFarsi/LazyInitialization","commit_stats":null,"previous_names":["bardiparsi/lazyinitialization","bardifarsi/lazyinitialization"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BardiFarsi%2FLazyInitialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BardiFarsi%2FLazyInitialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BardiFarsi%2FLazyInitialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BardiFarsi%2FLazyInitialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BardiFarsi","download_url":"https://codeload.github.com/BardiFarsi/LazyInitialization/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239727035,"owners_count":19687096,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["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"],"created_at":"2024-12-29T17:43:05.603Z","updated_at":"2025-02-19T19:43:11.819Z","avatar_url":"https://github.com/BardiFarsi.png","language":"C++","readme":"# Lazy Initialization with Multithreading in C++\n\n## Overview\nThis 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.\n\n## Requirements\n- C++20 compatible compiler\n- This code has been tested and confirmed to work with both Mingw g++ and Windows MSVC compilers. GCC compiler should be avoided.\n- Standard C++ libraries\n- Git\n\n## Usage\nTo use this solution, follow these steps:\n1. Clone the repository to your local machine:\n    ```bash\n    git clone https://github.com/BardiParsi/LazyInitialization.git\n    ```\n2. Navigate to the cloned directory:\n    ```bash\n    cd LazyInitialization1\n    ```\n3. Compile the source code:\n    ```bash\n    g++ -std=c++20 -pthread main.cpp -o main\n    ```\n4. Run the executable:\n    ```bash\n    ./main\n    ```\n\n## Implementation Details\nThe solution consists of the following key components:\n- `ExpensiveObject`: Represents an expensive object that is initialized lazily.\n- `LazyInitialization`: Manages the lazy initialization of `ExpensiveObject` using a mutex and atomic flags to ensure thread safety and efficient resource utilization.\n- `main`: Demonstrates the usage of `LazyInitialization` with multiple threads.\n\n## Code Explanation\n- `ExpensiveObject`: Simulates an expensive object that takes some time to initialize.\n- `LazyInitialization`: Implements lazy initialization using `std::once_flag` and `std::atomic\u003cbool\u003e`. The `useObject()` function ensures that the object is initialized only once and is thread-safe.\n- `main`: Creates a vector of multiple threads that attempt to use the `ExpensiveObject` simultaneously, demonstrating the effectiveness of lazy initialization and multithreading.\n\n## Contributing\nContributions are welcome! Feel free to submit issues or pull requests for any improvements or additional features.\n\n## License\nThis project is licensed under the GNU General Public License (GPL).\nAuthor: Bardi (Masoud) Parsi\nContact: farsi.masoud@gmail.com\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbardifarsi%2Flazyinitialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbardifarsi%2Flazyinitialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbardifarsi%2Flazyinitialization/lists"}