Skip to content

Commit

Permalink
Threads sleep in ConcurrentQueue after spinning too much.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcovington committed Oct 28, 2020
1 parent e5f6b44 commit e7ce82d
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ internal sealed class ConcurrentQueueSegment<T>
internal ConcurrentQueueSegment<T>? _nextSegment; // SOS's ThreadPool command depends on this name
#pragma warning restore 0649

internal int _sleep1Threshold = Thread.OptimalMaxSpinWaitsPerSpinIteration;

/// <summary>Creates the segment.</summary>
/// <param name="boundedLength">
/// The maximum number of elements the segment can contain. Must be a power of 2.
Expand Down Expand Up @@ -183,7 +185,7 @@ public bool TryDequeue([MaybeNullWhen(false)] out T item)
}

// Lost a race. Spin a bit, then try again.
spinner.SpinOnce(sleep1Threshold: -1);
spinner.SpinOnce(sleep1Threshold: _sleep1Threshold);
}
}

Expand Down Expand Up @@ -244,7 +246,7 @@ public bool TryPeek([MaybeNullWhen(false)] out T result, bool resultUsed)
}

// Lost a race. Spin a bit, then try again.
spinner.SpinOnce(sleep1Threshold: -1);
spinner.SpinOnce(sleep1Threshold: _sleep1Threshold);
}
}

Expand Down Expand Up @@ -301,7 +303,7 @@ public bool TryEnqueue(T item)
}

// Lost a race. Spin a bit, then try again.
spinner.SpinOnce(sleep1Threshold: -1);
spinner.SpinOnce(sleep1Threshold: _sleep1Threshold);
}
}

Expand Down

0 comments on commit e7ce82d

Please sign in to comment.