From 4be560fb0006e9b5dd67c7dd42c3897fb9bb876b Mon Sep 17 00:00:00 2001 From: Yuri Voinov Date: Sat, 24 Mar 2018 23:17:04 +0600 Subject: [PATCH] Conditional variable instead of sleep_for I'm trying to avoid some overhead when pool is idle or underload. Pls, advice me - is this way correct? Testing shows less overall pool latency, but I'm in doubt - this should not be serialization point for scaling. --- cond_variable_instead_sleep_for.patch | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cond_variable_instead_sleep_for.patch diff --git a/cond_variable_instead_sleep_for.patch b/cond_variable_instead_sleep_for.patch new file mode 100644 index 00000000..872366e0 --- /dev/null +++ b/cond_variable_instead_sleep_for.patch @@ -0,0 +1,46 @@ +--- worker.hpp Thu Mar 22 23:20:12 2018 ++++ worker.hpp Sat Mar 24 22:28:13 2018 +@@ -2,6 +2,8 @@ + + #include + #include ++#include ++#include + + namespace tp + { +@@ -78,6 +80,8 @@ + Queue m_queue; + std::atomic m_running_flag; + std::thread m_thread; ++ std::mutex m_conditional_mutex; ++ std::condition_variable m_conditional_lock; + }; + + +@@ -121,6 +125,7 @@ + inline void Worker::stop() + { + m_running_flag.store(false, std::memory_order_relaxed); ++ m_conditional_lock.notify_all(); + m_thread.join(); + } + +@@ -140,6 +145,7 @@ + template + inline bool Worker::post(Handler&& handler) + { ++ m_conditional_lock.notify_one(); + return m_queue.push(std::forward(handler)); + } + +@@ -171,7 +177,8 @@ + } + else + { +- std::this_thread::sleep_for(std::chrono::milliseconds(1)); ++ std::unique_lock lock(m_conditional_mutex); ++ m_conditional_lock.wait(lock); + } + } + }