From 23652409f6b04948fc9df0fb60eb269410488e80 Mon Sep 17 00:00:00 2001 From: q66 Date: Mon, 20 Mar 2017 02:35:50 +0100 Subject: [PATCH] use lock_guard where possible --- ostd/channel.hh | 6 +++--- ostd/concurrency.hh | 6 +++--- ostd/thread_pool.hh | 47 +++++++++++++++++++++++++-------------------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/ostd/channel.hh b/ostd/channel.hh index f2f4a32..bd90182 100644 --- a/ostd/channel.hh +++ b/ostd/channel.hh @@ -74,7 +74,7 @@ private: template void put(U &&val) { - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; if (p_closed) { throw channel_error{"put in a closed channel"}; } @@ -101,12 +101,12 @@ private: } bool is_closed() const { - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; return p_closed; } void close() { - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; p_closed = true; p_cond.notify_all(); } diff --git a/ostd/concurrency.hh b/ostd/concurrency.hh index 9b8eef4..e95c532 100644 --- a/ostd/concurrency.hh +++ b/ostd/concurrency.hh @@ -30,7 +30,7 @@ struct thread_scheduler { template void spawn(F &&func, A &&...args) { - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; p_threads.emplace_front(); auto it = p_threads.begin(); *it = std::thread{ @@ -53,7 +53,7 @@ struct thread_scheduler { private: void remove_thread(typename std::list::iterator it) { - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; std::thread t{std::exchange(p_dead, std::move(*it))}; if (t.joinable()) { t.join(); @@ -63,7 +63,7 @@ private: void join_all() { /* wait for all threads to finish */ - std::unique_lock l{p_lock}; + std::lock_guard l{p_lock}; if (p_dead.joinable()) { p_dead.join(); } diff --git a/ostd/thread_pool.hh b/ostd/thread_pool.hh index 2970f10..964b7e2 100644 --- a/ostd/thread_pool.hh +++ b/ostd/thread_pool.hh @@ -45,12 +45,13 @@ struct thread_pool { } void destroy() { - std::unique_lock l{p_lock}; - if (!p_running) { - return; + { + std::lock_guard l{p_lock}; + if (!p_running) { + return; + } + p_running = false; } - p_running = false; - l.unlock(); p_cond.notify_all(); for (auto &tid: p_thrs) { tid.join(); @@ -65,16 +66,18 @@ struct thread_pool { using R = std::result_of_t; if constexpr(std::is_same_v) { /* void-returning funcs return void */ - std::unique_lock l{p_lock}; - if (!p_running) { - throw std::runtime_error{"push on stopped thread_pool"}; - } - if constexpr(sizeof...(A) == 0) { - p_tasks.push(std::forward(func)); - } else { - p_tasks.push( - std::bind(std::forward(func), std::forward(args)...) - ); + { + std::lock_guard l{p_lock}; + if (!p_running) { + throw std::runtime_error{"push on stopped thread_pool"}; + } + if constexpr(sizeof...(A) == 0) { + p_tasks.push(std::forward(func)); + } else { + p_tasks.push( + std::bind(std::forward(func), std::forward(args)...) + ); + } } p_cond.notify_one(); } else { @@ -88,13 +91,15 @@ struct thread_pool { }; } auto ret = t.get_future(); - std::unique_lock l{p_lock}; - if (!p_running) { - throw std::runtime_error{"push on stopped thread_pool"}; + { + std::lock_guard l{p_lock}; + if (!p_running) { + throw std::runtime_error{"push on stopped thread_pool"}; + } + p_tasks.emplace([t = std::move(t)]() { + t(); + }); } - p_tasks.emplace([t = std::move(t)]() { - t(); - }); p_cond.notify_one(); return ret; }