WriterReaderPhaser.FlipPhase - Task.Yield().GetAwaiter().GetResult() is a Thread Pool Anti-Pattern
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- performance
Research direction
Start in Utilities/WriterReaderPhaser.cs and inspect FlipPhase, especially the synchronous Task.Yield() wait and its caughtUp loop. Compare the existing behavior with the issue's SpinWait approach, then run the repository's existing test suite to verify phase flipping still completes correctly.
Written by the indexing model from the issue text.
Description
File: Utilities/WriterReaderPhaser.cs
The spin-wait in FlipPhase uses:
Task.Yield().GetAwaiter().GetResult();
This is a synchronous block on an async yield — it queues the continuation back to the thread pool and then blocks the calling thread waiting for it, which is deeply counterproductive. The right approach for a spin-wait that wants to yield CPU time is:
// Prefer SpinWait for short-duration waits
var spinner = new SpinWait();
do {
spinner.SpinOnce(); // handles Thread.Yield / Thread.Sleep(0) / Thread.Sleep(1) progression automatically
caughtUp = isNextPhaseEven
? (_oddEndEpoch == startValueAtFlip)
: (_evenEndEpoch == startValueAtFlip);
} while (!caughtUp);
SpinWait is purpose-built for exactly this pattern and uses adaptive backoff — it spins on CPU first, then Thread.Yield(), then Thread.Sleep(1), automatically.
- Dominant language
- C#
- Stars
- 185
- Forks
- 31
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HdrHistogram/HdrHistogram.NET
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
HdrHistogram/HdrHistogram.NET#167 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
HdrHistogram/HdrHistogram.NET#166 ·
-
agent enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
HdrHistogram/HdrHistogram.NET#142 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
HdrHistogram/HdrHistogram.NET#156 ·
-
agent enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
HdrHistogram/HdrHistogram.NET#148 ·
All issues in HdrHistogram/HdrHistogram.NET
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·