我想知道 Mutex
对象是忙等待还是上下文切换(即拥有互斥锁的线程是否进入休眠状态并稍后被中断唤醒),或者它是架构依赖(即你的机器有多少核)?我希望它实际上可以切换上下文。提前谢谢你。
最佳答案
根据文档,Mutex.WaitOne
“阻塞当前线程,直到当前 WaitHandle 收到信号”,这意味着它进入休眠状态。
在内部,WaitHandle.WaitOne
将调用 WaitForSingleObjectEx
来自 Windows API,它:
Waits until the specified object is in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses.
此外,根据关于 Context Switches 的另一份文件
When a running thread needs to wait, it relinquishes the remainder of its time slice.
关于c# - C# 中的 Mutex 忙等待吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460569/