fix(thread-pool): use std::call_once for thread-safe diagnostics init#669
Merged
Conversation
thread_pool::diagnostics() used lazy initialization of a mutable unique_ptr without synchronization. Concurrent first calls from multiple threads could race on make_unique. Replace with std::call_once + std::once_flag for guaranteed thread-safe one-time initialization. Add mutable std::once_flag diagnostics_init_flag_ member and #include <mutex> to the header. Fix pre-existing broken TOC anchor in docs/API_REFERENCE.kr.md (heading renamed from Lock-Free to Concurrent). Closes #668
Contributor
📊 Performance Benchmark ResultsPerformance Benchmark ReportNo benchmark data available. ℹ️ No baseline reference availableThis is the first benchmark run or baseline file is missing. |
kcenon
added a commit
that referenced
this pull request
Apr 13, 2026
…#669) thread_pool::diagnostics() used lazy initialization of a mutable unique_ptr without synchronization. Concurrent first calls from multiple threads could race on make_unique. Replace with std::call_once + std::once_flag for guaranteed thread-safe one-time initialization. Add mutable std::once_flag diagnostics_init_flag_ member and #include <mutex> to the header. Fix pre-existing broken TOC anchor in docs/API_REFERENCE.kr.md (heading renamed from Lock-Free to Concurrent). Closes #668
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Summary
Replaces the unsynchronized lazy initialization of
thread_pool::diagnostics_withstd::call_onceto prevent a data race when multiple threads calldiagnostics()concurrently for the first time.Change Type
Affected Components
include/kcenon/thread/core/thread_pool.h— Addstd::once_flagmembersrc/impl/thread_pool/thread_pool.cpp— Bothdiagnostics()overloadsWhy
Problem Solved
thread_pool::diagnostics()used a bareif (!diagnostics_)check followed bymake_uniquewithout any synchronization. Two threads callingdiagnostics()simultaneously for the first time could both observe!diagnostics_as true and both attempt allocation, causing a data race (undefined behavior). Theconstoverload additionally usedconst_castwhich is UB if the pool was originally declaredconst.Related Issues
Where
Files Changed
include/kcenon/thread/core/thread_pool.hmutable std::once_flag diagnostics_init_flag_,#include <mutex>src/impl/thread_pool/thread_pool.cppifwithstd::call_oncein both overloadsdocs/API_REFERENCE.kr.mdHow
Implementation Details
Breaking Changes
None —
diagnostics()return type and behavior unchanged.