Loading src/threadpool.h +30 −1 Changes for src/threadpool.h: 30 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <functional> #include <mutex> #include <condition_variable> #include <chrono> namespace netplus { Loading Loading @@ -62,8 +63,27 @@ public: _stopping = true; } _cv.notify_all(); // A worker can be stuck inside a submitted task that never returns // (e.g. a callback blocked on an unresponsive network peer with no // timeout). A plain join() would then hang process shutdown // forever, since this pool is a process-lifetime singleton // (quicDispatchPool()) destructed from global teardown. Give // workers a bounded grace period to drain; past that, detach the // stragglers so the process can still exit — the OS reclaims a // detached thread when the process terminates. static constexpr auto SHUTDOWN_GRACE = std::chrono::seconds(5); { std::unique_lock<std::mutex> lk(_mtx); _done_cv.wait_for(lk, SHUTDOWN_GRACE, [this] { return _finished >= _workers.size(); }); } bool all_finished = _finished >= _workers.size(); for (auto& t : _workers) { if (t.joinable()) t.join(); if (!t.joinable()) continue; if (all_finished) t.join(); else t.detach(); } } Loading Loading @@ -91,6 +111,9 @@ private: // Only reachable via _stopping — drain fully before exiting // (queued work still runs; shutdown just stops accepting // the wait once there's nothing left). ++_finished; lk.unlock(); _done_cv.notify_all(); return; } task = std::move(_tasks.front()); Loading @@ -113,6 +136,12 @@ private: std::mutex _mtx; std::condition_variable _cv; bool _stopping = false; // Shutdown-hang guard: counts workers that have returned from // workerLoop(), so the destructor can bound how long it waits before // giving up on a worker stuck in a never-returning task. size_t _finished = 0; std::condition_variable _done_cv; }; } // namespace netplus Loading
src/threadpool.h +30 −1 Changes for src/threadpool.h: 30 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <functional> #include <mutex> #include <condition_variable> #include <chrono> namespace netplus { Loading Loading @@ -62,8 +63,27 @@ public: _stopping = true; } _cv.notify_all(); // A worker can be stuck inside a submitted task that never returns // (e.g. a callback blocked on an unresponsive network peer with no // timeout). A plain join() would then hang process shutdown // forever, since this pool is a process-lifetime singleton // (quicDispatchPool()) destructed from global teardown. Give // workers a bounded grace period to drain; past that, detach the // stragglers so the process can still exit — the OS reclaims a // detached thread when the process terminates. static constexpr auto SHUTDOWN_GRACE = std::chrono::seconds(5); { std::unique_lock<std::mutex> lk(_mtx); _done_cv.wait_for(lk, SHUTDOWN_GRACE, [this] { return _finished >= _workers.size(); }); } bool all_finished = _finished >= _workers.size(); for (auto& t : _workers) { if (t.joinable()) t.join(); if (!t.joinable()) continue; if (all_finished) t.join(); else t.detach(); } } Loading Loading @@ -91,6 +111,9 @@ private: // Only reachable via _stopping — drain fully before exiting // (queued work still runs; shutdown just stops accepting // the wait once there's nothing left). ++_finished; lk.unlock(); _done_cv.notify_all(); return; } task = std::move(_tasks.front()); Loading @@ -113,6 +136,12 @@ private: std::mutex _mtx; std::condition_variable _cv; bool _stopping = false; // Shutdown-hang guard: counts workers that have returned from // workerLoop(), so the destructor can bound how long it waits before // giving up on a worker stuck in a never-returning task. size_t _finished = 0; std::condition_variable _done_cv; }; } // namespace netplus