use lock_guard where possible
parent
e7855faeb1
commit
23652409f6
|
@ -74,7 +74,7 @@ private:
|
|||
|
||||
template<typename U>
|
||||
void put(U &&val) {
|
||||
std::unique_lock<std::mutex> l{p_lock};
|
||||
std::lock_guard<std::mutex> 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<std::mutex> l{p_lock};
|
||||
std::lock_guard<std::mutex> l{p_lock};
|
||||
return p_closed;
|
||||
}
|
||||
|
||||
void close() {
|
||||
std::unique_lock<std::mutex> l{p_lock};
|
||||
std::lock_guard<std::mutex> l{p_lock};
|
||||
p_closed = true;
|
||||
p_cond.notify_all();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ struct thread_scheduler {
|
|||
|
||||
template<typename F, typename ...A>
|
||||
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();
|
||||
auto it = p_threads.begin();
|
||||
*it = std::thread{
|
||||
|
@ -53,7 +53,7 @@ struct thread_scheduler {
|
|||
|
||||
private:
|
||||
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))};
|
||||
if (t.joinable()) {
|
||||
t.join();
|
||||
|
@ -63,7 +63,7 @@ private:
|
|||
|
||||
void join_all() {
|
||||
/* 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()) {
|
||||
p_dead.join();
|
||||
}
|
||||
|
|
|
@ -45,12 +45,13 @@ struct thread_pool {
|
|||
}
|
||||
|
||||
void destroy() {
|
||||
std::unique_lock<std::mutex> l{p_lock};
|
||||
if (!p_running) {
|
||||
return;
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<F(A...)>;
|
||||
if constexpr(std::is_same_v<R, void>) {
|
||||
/* void-returning funcs return void */
|
||||
std::unique_lock<std::mutex> 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<F>(func));
|
||||
} else {
|
||||
p_tasks.push(
|
||||
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
||||
);
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<F>(func));
|
||||
} else {
|
||||
p_tasks.push(
|
||||
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
||||
);
|
||||
}
|
||||
}
|
||||
p_cond.notify_one();
|
||||
} else {
|
||||
|
@ -88,13 +91,15 @@ struct thread_pool {
|
|||
};
|
||||
}
|
||||
auto ret = t.get_future();
|
||||
std::unique_lock<std::mutex> l{p_lock};
|
||||
if (!p_running) {
|
||||
throw std::runtime_error{"push on stopped thread_pool"};
|
||||
{
|
||||
std::lock_guard<std::mutex> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue