Medium
Copying a global std::shared_ptr while another thread reassigns it
A config-reload thread periodically replaces a global std::shared_ptr while many request-handler threads copy it. Nothing else synchronizes them. What is the status of this code?
main.cpp
std::shared_ptr<const Config> g_cfg = std::make_shared<const Config>();
void reloader() { // thread A, periodically
auto fresh = std::make_shared<const Config>(load_from_disk());
g_cfg = fresh; // publish the new config
}
void handler() { // threads B..Z, per request
std::shared_ptr<const Config> local = g_cfg; // take a reference
use(*local);
}47 of 51 in Concurrency