是否有效率
SpinWait.SpinUntil(() => myPredicate(), 10000)
Run Code Online (Sandbox Code Playgroud)
超时10000毫秒
要么
Thread.Sleep在相同条件下使用轮询是否更有效例如,以下SleepWait函数的行:
public bool SleepWait(int timeOut)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (!myPredicate() && stopwatch.ElapsedMilliseconds < timeOut)
{
Thread.Sleep(50)
}
return myPredicate()
}
Run Code Online (Sandbox Code Playgroud)
我担心如果我们谈论超过1秒的超时,SpinWait的所有收益可能都不是一个好的使用模式?这是一个有效的假设吗?
您更喜欢哪种方法?为什么?还有另一种更好的方法吗?
更新 - 变得更具体:
有没有办法让BlockingCollection Pulse在达到有界容量时成为睡眠线程?正如Marc Gravel建议的那样,我宁愿避免忙碌等待.