C# 中互斥体的使用
我对c#中的线程和一般情况有点陌生, 在我的程序中,我使用 mutex
只允许 1 个线程进入关键部分,并且出于未知原因,通过执行一些 CW 打印,我可以看到超过 1 个线程正在进入我的关键部分,这是我的代码:
Mutex m = new Mutex();
m.WaitOne();
<C.S> // critical section here
m.ReleaseMutex();
我非常想知道我是否在这里犯了错误,提前感谢您的帮助。
编辑:
我的代码包含类,所以它基本上看起来更像这样:
public class test
{
private mutex m;
public test()
{
m = new mutex();
}
public func()
{
m.WaitOne();
<C.S> // critical section here
m.ReleaseMutex();
}
}
I am a bit new in threading in c# and on general,
in my program I am using mutex
to allow only 1 thread getting inside a critical section and for unknown reason with doing some cw prints I can see that more than 1 thread is getting inside my critical section and this is my code :
Mutex m = new Mutex();
m.WaitOne();
<C.S> // critical section here
m.ReleaseMutex();
I would very much like to know if I am doing a mistake here thanks in advance for your kind help.
EDIT:
My code include classes so it basically looks more like this:
public class test
{
private mutex m;
public test()
{
m = new mutex();
}
public func()
{
m.WaitOne();
<C.S> // critical section here
m.ReleaseMutex();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里的问题是所有调用者都使用不同互斥体;您需要将锁定对象共享,通常是将其设置为字段。例如,切换到更简单的
lock
隐喻:或使用互斥体:
The problem here is that all your callers are using a different mutex; you need the locking object to be shared, usually by making it a field. For example, and switching to a simpler
lock
metaphor:or using the mutex:
看起来您为每个线程提供了自己的互斥体。那是行不通的。
在大多数情况下,互斥量是多余的。您只需要:
It looks like you give each Thread its own Mutex. That won't work.
And a Mutex is overkill in most situations. You only need:
这种模式根本没有锁定。每个线程都会创建一个新的 Mutex 对象并立即拥有它的锁。其他线程本身创建并使用新的互斥体。
考虑使用常规的lock()!
其中 _lockobject 是类中的一个简单的私有变量:
编辑:感谢评论者!存在 lock(this) 可能很危险的情况。所以我删除了它。
This pattern does no locking at all. Every thread creates a new Mutex object and immediately owns the lock for it. Other threads create and use a new Mutex itself.
Consider using a regular lock()!
where _lockobject is a simple private variable in your class:
Edit: thanks to the commenters! Situations exist, where lock(this) can be dangerous. So I removed that.
互斥体用于识别运行的应用程序实例。
Mutex use to identify run app instance.
我可以对已接受的答案进行更正吗?
May i add a correction to the accepted answer?