diff --git a/include/opendht/thread_pool.h b/include/opendht/thread_pool.h index fe80b7f2..8b0ebfb2 100644 --- a/include/opendht/thread_pool.h +++ b/include/opendht/thread_pool.h @@ -63,6 +63,7 @@ public: void stop(bool wait = true); void join(); + void detach(); private: std::mutex lock_ {}; diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 8d3c9291..7fab7c11 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -85,7 +85,7 @@ ThreadPool::run(std::function&& cb) auto waitCond = [&](){ return not running_ or not tasks_.empty(); }; if (permanent_thread) cv_.wait(l, waitCond); - else + else cv_.wait_for(l, e, waitCond); readyThreads_--; if (not running_ or tasks_.empty()) @@ -166,6 +166,16 @@ ThreadPool::join() tasks_ = {}; } +void +ThreadPool::detach() +{ + stop(false); + for (auto& t : threads_) + t->detach(); + threads_.clear(); + tasks_ = {}; +} + void Executor::run(std::function&& task) {