use lock_guard where possible
This commit is contained in:
parent
e7855faeb1
commit
23652409f6
|
@ -74,7 +74,7 @@ private:
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
void put(U &&val) {
|
void put(U &&val) {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (p_closed) {
|
if (p_closed) {
|
||||||
throw channel_error{"put in a closed channel"};
|
throw channel_error{"put in a closed channel"};
|
||||||
}
|
}
|
||||||
|
@ -101,12 +101,12 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_closed() const {
|
bool is_closed() const {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
return p_closed;
|
return p_closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
p_closed = true;
|
p_closed = true;
|
||||||
p_cond.notify_all();
|
p_cond.notify_all();
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ struct thread_scheduler {
|
||||||
|
|
||||||
template<typename F, typename ...A>
|
template<typename F, typename ...A>
|
||||||
void spawn(F &&func, A &&...args) {
|
void spawn(F &&func, A &&...args) {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
p_threads.emplace_front();
|
p_threads.emplace_front();
|
||||||
auto it = p_threads.begin();
|
auto it = p_threads.begin();
|
||||||
*it = std::thread{
|
*it = std::thread{
|
||||||
|
@ -53,7 +53,7 @@ struct thread_scheduler {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void remove_thread(typename std::list<std::thread>::iterator it) {
|
void remove_thread(typename std::list<std::thread>::iterator it) {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
std::thread t{std::exchange(p_dead, std::move(*it))};
|
std::thread t{std::exchange(p_dead, std::move(*it))};
|
||||||
if (t.joinable()) {
|
if (t.joinable()) {
|
||||||
t.join();
|
t.join();
|
||||||
|
@ -63,7 +63,7 @@ private:
|
||||||
|
|
||||||
void join_all() {
|
void join_all() {
|
||||||
/* wait for all threads to finish */
|
/* wait for all threads to finish */
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (p_dead.joinable()) {
|
if (p_dead.joinable()) {
|
||||||
p_dead.join();
|
p_dead.join();
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,12 +45,13 @@ struct thread_pool {
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy() {
|
void destroy() {
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
{
|
||||||
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (!p_running) {
|
if (!p_running) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
p_running = false;
|
p_running = false;
|
||||||
l.unlock();
|
}
|
||||||
p_cond.notify_all();
|
p_cond.notify_all();
|
||||||
for (auto &tid: p_thrs) {
|
for (auto &tid: p_thrs) {
|
||||||
tid.join();
|
tid.join();
|
||||||
|
@ -65,7 +66,8 @@ struct thread_pool {
|
||||||
using R = std::result_of_t<F(A...)>;
|
using R = std::result_of_t<F(A...)>;
|
||||||
if constexpr(std::is_same_v<R, void>) {
|
if constexpr(std::is_same_v<R, void>) {
|
||||||
/* void-returning funcs return void */
|
/* void-returning funcs return void */
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
{
|
||||||
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (!p_running) {
|
if (!p_running) {
|
||||||
throw std::runtime_error{"push on stopped thread_pool"};
|
throw std::runtime_error{"push on stopped thread_pool"};
|
||||||
}
|
}
|
||||||
|
@ -76,6 +78,7 @@ struct thread_pool {
|
||||||
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
p_cond.notify_one();
|
p_cond.notify_one();
|
||||||
} else {
|
} else {
|
||||||
/* non-void-returning funcs return a future */
|
/* non-void-returning funcs return a future */
|
||||||
|
@ -88,13 +91,15 @@ struct thread_pool {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
auto ret = t.get_future();
|
auto ret = t.get_future();
|
||||||
std::unique_lock<std::mutex> l{p_lock};
|
{
|
||||||
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (!p_running) {
|
if (!p_running) {
|
||||||
throw std::runtime_error{"push on stopped thread_pool"};
|
throw std::runtime_error{"push on stopped thread_pool"};
|
||||||
}
|
}
|
||||||
p_tasks.emplace([t = std::move(t)]() {
|
p_tasks.emplace([t = std::move(t)]() {
|
||||||
t();
|
t();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
p_cond.notify_one();
|
p_cond.notify_one();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue