Skip to content

Commit

Permalink
notify only if there exist therads
Browse files Browse the repository at this point in the history
and limit lifetime of lock
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Jan 9, 2017
1 parent bf6b302 commit ccff5ae
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/thread_pool.hpp
Expand Up @@ -109,12 +109,13 @@ class Thread_pool {
if (threads.empty()) {
(*ptask)();
} else {
std::unique_lock<std::mutex> lock(mutex);
tasks.emplace([ptask](){ (*ptask)(); });
{
std::unique_lock<std::mutex> lock(mutex);
tasks.emplace([ptask](){ (*ptask)(); });
}
condition.notify_one();
}

condition.notify_one();

return fut;
}

Expand All @@ -125,8 +126,10 @@ class Thread_pool {
if (threads.empty()) {
task();
} else {
std::unique_lock<std::mutex> lock(mutex);
tasks.emplace(std::forward<Task>(task));
{
std::unique_lock<std::mutex> lock(mutex);
tasks.emplace(std::forward<Task>(task));
}
condition.notify_one();
}
}
Expand Down

0 comments on commit ccff5ae

Please sign in to comment.