Skip to content

Commit

Permalink
Fixed a rare std::bad_function_call exception
Browse files Browse the repository at this point in the history
Since m_task was initialized after threads are created, there's a small time window where workers could encounter an uninitialized m_task.

Possible complementary fix for #44
  • Loading branch information
ata4 committed Feb 19, 2018
1 parent 1dd6c78 commit ab56f66
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions core/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class Parallel
Parallel(uint32_t num_workers) {
std::unique_lock<std::mutex> ul(m_mutex);

// give workers an empty task
m_task = [](uint32_t) {};

// create worker threads
for (uint32_t worker_id = 0; worker_id < num_workers; worker_id++) {
m_workers.emplace_back(std::thread(&Parallel::do_work, this, worker_id));
}

// give workers an empty task and wait for them to finish it to
// make sure they're ready
m_task = [](uint32_t){};
// wait for workers to finish task to make sure they're ready
m_workers_active = m_workers.size();
m_signal_done.wait(ul);
}
Expand Down

0 comments on commit ab56f66

Please sign in to comment.