Fixing thread pool shutdown so the kill flag is set inside the mutex, preventing a race. This could cause very rare lockups on the previous code.

Also for more safety to prevent lockups on buggy C run-times, adding a 1s timeout in the thread pool condition variable wait.
This commit is contained in:
Richard Geldreich
2025-09-05 21:13:04 -04:00
parent 2ac62fb349
commit 2c6712697c

View File

@@ -2248,7 +2248,10 @@ namespace basisu
debug_printf("job_pool::~job_pool\n");
// Notify all workers that they need to die right now.
m_kill_flag.store(true);
{
std::lock_guard<std::mutex> lk(m_mutex);
m_kill_flag.store(true);
}
m_has_work.notify_all();
@@ -2338,17 +2341,27 @@ namespace basisu
m_num_active_workers.fetch_add(1);
while (true)
while (!m_kill_flag)
{
std::unique_lock<std::mutex> lock(m_mutex);
// Wait for any jobs to be issued.
#if 0
m_has_work.wait(lock, [this] { return m_kill_flag || m_queue.size(); } );
#else
// For more safety vs. buggy RTL's. Worse case we stall for a second vs. locking up forever if something goes wrong.
m_has_work.wait_for(lock, std::chrono::milliseconds(1000), [this] {
return m_kill_flag || !m_queue.empty();
});
#endif
// Check to see if we're supposed to exit.
if (m_kill_flag)
break;
if (m_queue.empty())
continue;
// Get the job and execute it.
std::function<void()> job(m_queue.back());
m_queue.pop_back();