diff --git a/repertory/librepertory/include/comm/packet/client_pool.hpp b/repertory/librepertory/include/comm/packet/client_pool.hpp index 8b2ef443..d62c1abc 100644 --- a/repertory/librepertory/include/comm/packet/client_pool.hpp +++ b/repertory/librepertory/include/comm/packet/client_pool.hpp @@ -52,7 +52,7 @@ private: work_queue(work_queue &&) = delete; std::deque> actions; - std::chrono::steady_clock::time_point modified{ + std::atomic modified{ std::chrono::steady_clock::now(), }; std::mutex mutex; diff --git a/repertory/librepertory/src/comm/packet/client_pool.cpp b/repertory/librepertory/src/comm/packet/client_pool.cpp index 3b2b910f..388bba70 100644 --- a/repertory/librepertory/src/comm/packet/client_pool.cpp +++ b/repertory/librepertory/src/comm/packet/client_pool.cpp @@ -50,8 +50,8 @@ void client_pool::pool::work_queue::work_thread() { unique_mutex_lock lock(mutex); const auto unlock_and_notify = [this, &lock]() { - notify.notify_all(); lock.unlock(); + notify.notify_all(); }; unlock_and_notify(); @@ -77,9 +77,7 @@ void client_pool::pool::work_queue::work_thread() { while (not shutdown) { lock.lock(); if (actions.empty()) { - if (not shutdown) { - notify.wait_for(lock, std::chrono::seconds(5U)); - } + notify.wait(lock, [this]() { return shutdown || not actions.empty(); }); unlock_and_notify(); continue; } @@ -99,6 +97,9 @@ void client_pool::pool::execute(std::uint64_t thread_id, worker_callback worker, worker_complete_callback worker_complete) { REPERTORY_USES_FUNCTION_NAME(); + auto job = std::make_shared(std::move(worker), + std::move(worker_complete)); + unique_mutex_lock pool_lock(pool_mtx_); if (pool_queues_[thread_id] == nullptr) { pool_queues_[thread_id] = std::make_shared(); @@ -107,12 +108,10 @@ void client_pool::pool::execute(std::uint64_t thread_id, worker_callback worker, auto pool_queue = pool_queues_[thread_id]; pool_lock.unlock(); - auto job = std::make_shared(std::move(worker), - std::move(worker_complete)); - - mutex_lock queue_lock(pool_queue->mutex); - pool_queue->actions.emplace_back(std::move(job)); pool_queue->modified = std::chrono::steady_clock::now(); + + unique_mutex_lock queue_lock(pool_queue->mutex); + pool_queue->actions.emplace_back(std::move(job)); pool_queue->notify.notify_all(); } @@ -124,7 +123,7 @@ void client_pool::pool::remove_expired() { pool_queues_.begin(), pool_queues_.end(), std::unordered_map>(), [&now](auto &&res, auto &&entry) -> auto { - auto duration = now - entry.second->modified; + auto duration = now - entry.second->modified.load(); if (std::chrono::duration_cast(duration) >= std::chrono::minutes(2U)) { res[entry.first] = entry.second;