Skip to content
Open
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
77 changes: 43 additions & 34 deletions src/ThreadWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace execq
void shutdown();

private:
std::atomic_bool m_shouldQuit { false };
std::atomic_bool m_checkNextTask { false };
bool m_shouldQuit = false;
bool m_checkNextTask = false;
std::condition_variable m_condition;
std::mutex m_mutex;
std::unique_ptr<std::thread> m_thread;
Expand Down Expand Up @@ -82,58 +82,67 @@ execq::impl::ThreadWorker::~ThreadWorker()

bool execq::impl::ThreadWorker::notifyWorker()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_checkNextTask)
{
return false;
}

m_checkNextTask = true;
if (!m_thread)
{
m_thread.reset(new std::thread(&ThreadWorker::threadMain, this));
std::lock_guard<std::mutex> lock(m_mutex);
if (m_checkNextTask)
{
return false;
}

m_checkNextTask = true;
if (!m_thread)
{
m_thread.reset(new std::thread(&ThreadWorker::threadMain, this));
}
}

m_condition.notify_one();

return true;
}

void execq::impl::ThreadWorker::shutdown()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_shouldQuit = true;
{
std::lock_guard<std::mutex> lock(m_mutex);
m_shouldQuit = true;
}
m_condition.notify_one();
}

void execq::impl::ThreadWorker::threadMain()
{
while (true)
{
if (m_shouldQuit)
{
break;
}

m_checkNextTask = false;
Task task = m_provider.nextTask();
if (task.valid())
{
task();
continue;
std::unique_lock<std::mutex> lock(m_mutex);
while (!m_checkNextTask && !m_shouldQuit)
{
m_condition.wait(lock);
}

if (m_shouldQuit)
{
break;
}
}

std::unique_lock<std::mutex> lock(m_mutex);
if (m_checkNextTask)

while (true)
{
continue;
Task task = m_provider.nextTask();
if (task.valid())
{
task();
}
else
{
break;
}
}

if (m_shouldQuit)

{
break;
std::lock_guard<std::mutex> lock(m_mutex);
m_checkNextTask = false;
}

m_condition.wait(lock);
}
}