Skip to content

Commit

Permalink
Fixing windows static thread pool destructor deadlock.
Browse files Browse the repository at this point in the history
On a windows build, the thread pool destructor will deadlock after a period of idle time. This is happening because m_count has become sufficiently large that the YieldProcessor loop will block for minutes at a time.

Flipping the next_spin_will_yield check fixes this behavior, and I imagine was the intended behavior in the first place. This results in the lighter YieldProcessor calls being hit first, followed by the heavier OS Sleep calls, which continue until the spin resets or m_count wraps.
  • Loading branch information
loopunit committed Mar 21, 2019
1 parent 1cd6f37 commit 242db2b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/spin_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace cppcoro
#if CPPCORO_OS_WINNT
// Spin strategy taken from .NET System.SpinWait class.
// I assume the Microsoft guys knew what they're doing.
if (next_spin_will_yield())
if (!next_spin_will_yield())
{
// CPU-level pause
// Allow other hyper-threads to run while we busy-wait.
Expand Down

1 comment on commit 242db2b

@lewissbaker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding this! Did you want to submit a PR?
If not, I can apply the fix manually.

Please sign in to comment.