c# - C# 中的 Mutex 忙等待吗?

标签 c# .net multithreading mutex

我想知道 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/

相关文章:

c# - 如何在jquery中计算文件上传进度?

multithreading - 一个套接字连接多个命名空间

c# - 如何使用 javascript 将 C# 变量分配给 asp 元素

c# - 根据其他属性设置属性的方式

c# - 如何确定 .NET 类型是否为自定义结构?

c# - DbMigrator - 详细的代码优先迁移

multithreading - 尝试将 AddMessage 批量添加到 Azure 队列

wpf - WPF 中的 100% 响应式 UI

c# - 当所有其他对象暂停时移动角色

c# - 如何确定 Web 应用程序当前是否正在运行