From 15d0afdab368bd7ed3baff7e4487b8f51bbbaab3 Mon Sep 17 00:00:00 2001 From: Sever Topan Date: Thu, 29 Mar 2018 13:59:23 -0400 Subject: [PATCH] Review edits. --- include/thread_pool/rouser.hpp | 18 +++++++++++------- include/thread_pool/worker.hpp | 6 +++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/thread_pool/rouser.hpp b/include/thread_pool/rouser.hpp index f1899c53..326894ce 100644 --- a/include/thread_pool/rouser.hpp +++ b/include/thread_pool/rouser.hpp @@ -27,7 +27,7 @@ class Rouser final /** * @brief State An Enum representing the Rouser thread state. */ - enum State + enum class State { Initialized, Running, @@ -71,9 +71,9 @@ class Rouser final /** * @brief start Create the executing thread and start tasks execution. - * @param workers A pointer to the vector containing sibling workers for performing round robin work stealing. - * @param idle_workers A pointer to the slotted bag containing all idle workers. - * @param num_busy_waiters A pointer to the atomic busy waiter counter. + * @param workers A reference to the vector containing sibling workers for performing round robin work stealing. + * @param idle_workers A reference to the slotted bag containing all idle workers. + * @param num_busy_waiters A reference to the atomic busy waiter counter. * @note The parameters passed into this function generally relate to the global thread pool state. */ template class Queue> @@ -82,6 +82,8 @@ class Rouser final /** * @brief stop Stop all worker's thread and stealing activity. * Waits until the executing thread becomes finished. + * @note Stop may only be called once start() has been invoked. + * Repeated successful calls to stop() will be no-ops after the first. */ void stop(); @@ -89,9 +91,9 @@ class Rouser final /** * @brief threadFunc Executing thread function. - * @param workers A pointer to the vector containing sibling workers for performing round robin work stealing. - * @param idle_workers A pointer to the slotted bag containing all idle workers. - * @param num_busy_waiters A pointer to the atomic busy waiter counter. + * @param workers A reference to the vector containing sibling workers for performing round robin work stealing. + * @param idle_workers A reference to the slotted bag containing all idle workers. + * @param num_busy_waiters A reference to the atomic busy waiter counter. */ template class Queue> void threadFunc(std::vector>>& workers, SlottedBag& idle_workers, std::atomic& num_busy_waiters); @@ -144,6 +146,8 @@ inline void Rouser::stop() auto expectedState = State::Running; if (m_state.compare_exchange_strong(expectedState, State::Stopped, std::memory_order_acq_rel)) m_thread.join(); + else if (expectedState == State::Initialized) + throw std::runtime_error("Cannot stop Rouser: stop may only be calld after the Rouser has been started."); } template class Queue> diff --git a/include/thread_pool/worker.hpp b/include/thread_pool/worker.hpp index 81acc501..5af584fa 100644 --- a/include/thread_pool/worker.hpp +++ b/include/thread_pool/worker.hpp @@ -43,7 +43,7 @@ class Worker final /** * @brief State An Enum representing the Rouser thread state. */ - enum State + enum class State { Initialized, Running, @@ -100,6 +100,8 @@ class Worker final /** * @brief stop Stop all worker's thread and stealing activity. * Waits until the executing thread becomes finished. + * @note Stop may only be called once start() has been invoked. + * Repeated successful calls to stop() will be no-ops after the first. */ void stop(); @@ -245,6 +247,8 @@ inline void Worker::stop() wake(); m_thread.join(); } + else if (expectedState == State::Initialized) + throw std::runtime_error("Cannot stop Worker: stop may only be calld after the Worker has been started."); } template class Queue>