Skip to content

Commit 833f155

Browse files
committed
Fix server shutdown on Windows 7 and 8
std::condition_variable is broken there
1 parent 9c24f68 commit 833f155

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

Server/mods/deathmatch/logic/CGame.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ CGame::~CGame()
422422
SAFE_DELETE(m_pASE);
423423
SAFE_RELEASE(m_pHqComms);
424424
CSimControl::Shutdown();
425+
CThreadPool::getDefaultThreadPool().shutdown();
425426

426427
// Clear our global pointer
427428
g_pGame = NULL;

Shared/sdk/SharedUtil.ThreadPool.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,35 @@ namespace SharedUtil
7171
return res;
7272
}
7373

74-
~CThreadPool()
74+
void shutdown()
7575
{
76+
if (m_exit)
77+
return;
78+
79+
// Ensure every thread receives the exit state, and discard all remaining tasks.
80+
{
81+
std::unique_lock<std::mutex> lock(m_mutex);
82+
m_exit = true;
83+
84+
while (!m_tasks.empty())
85+
{
86+
// Run each task but skip execution of the actual function (-> just delete the task)
87+
auto task = std::move(m_tasks.front());
88+
task(true);
89+
}
90+
}
91+
7692
// Notify all threads to exit
77-
m_exit = true;
7893
m_cv.notify_all();
94+
7995
// Wait for threads to end
8096
for (std::thread& worker : m_vecThreads)
81-
{
8297
worker.join();
83-
}
84-
// Cleanup
85-
do
86-
{
87-
if (m_tasks.empty())
88-
break;
89-
// Run each task but skip execution of the actual
90-
// function (-> just delete the task)
91-
auto task = std::move(m_tasks.front());
92-
task(true);
93-
} while (true);
98+
}
99+
100+
~CThreadPool()
101+
{
102+
shutdown();
94103
}
95104

96105
static CThreadPool& getDefaultThreadPool()

0 commit comments

Comments
 (0)