Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the problem Worker thread cannot exit after shutdown #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/SafeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class SafeQueue {
}

SafeQueue(SafeQueue& other) {
//TODO:
std::unique_lock<std::mutex> lock(other.m_mutex);
m_queue = other.m_queue;
}

~SafeQueue() {
Expand Down Expand Up @@ -49,4 +50,4 @@ class SafeQueue {
m_queue.pop();
return true;
}
};
};
13 changes: 10 additions & 3 deletions include/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ class ThreadPool {
void operator()() {
std::function<void()> func;
bool dequeued;
while (!m_pool->m_shutdown) {
while (true) {
{
std::unique_lock<std::mutex> lock(m_pool->m_conditional_mutex);
if (m_pool->m_shutdown)
{
break;
}
if (m_pool->m_queue.empty()) {
m_pool->m_conditional_lock.wait(lock);
}
Expand Down Expand Up @@ -64,8 +68,11 @@ class ThreadPool {

// Waits until threads finish their current task and shutdowns the pool
void shutdown() {
m_shutdown = true;
m_conditional_lock.notify_all();
{
std::unique_lock<std::mutex> lock(m_conditional_mutex);
m_shutdown = true;
m_conditional_lock.notify_all();
}

for (int i = 0; i < m_threads.size(); ++i) {
if(m_threads[i].joinable()) {
Expand Down