简体   繁体   English

AutoResetEvent 和 Mutex 有什么区别

[英]Whats is the difference between AutoResetEvent and Mutex

I am new to these concepts.我对这些概念很陌生。 But as i am going deeper in threading i am getting confused.但是当我深入研究threading我感到困惑。

What is the significance of mutex , semaphore over autoresetevent . mutex semaphoresemaphoreautoresetevent的意义是什么。

Only difference i came to know with studies is that a mutex can perform across process operations.我通过研究了解到的唯一区别是mutex可以跨进程操作执行。 If this is the case why it does not have same method as Set , Reset , WaitOne .如果是这种情况,为什么它没有与SetResetWaitOne相同的方法。

Can we replace the AutoResetEvent with mutex and vice versa?我们可以用mutex替换AutoResetEvent ,反之亦然吗?

Please solve this puzzle.请解开这个谜题。

Different concept - a Mutex is an exclusive token;不同的概念 - Mutex是一个独占令牌; only one person can have it;只有一个人可以拥有它; when they release it, somebody else can fight over it.当他们释放它时,其他人可以为它而战。 An AutoResetEvent is a gate that allows exactly one person through before closing, and which is operated by a button that is separate to the queue of people wanting to go through. AutoResetEvent是一个在关闭前只允许一个人通过的门,它由一个按钮操作,该按钮与想要通过的人的队列分开。 When they pass through the gate immediately closes.当他们穿过大门时,大门立即关闭。

It depends.这取决于。

In common, AutoResetEvent and Mutex can be replaced, AutoResetEvent.WaitOne = Mutex.WaitOne and AutoResetEvent.Set = Mutex.ReleaseMutex.共同点,AutoResetEvent 和 Mutex 可以替换,AutoResetEvent.WaitOne = Mutex.WaitOne 和 AutoResetEvent.Set = Mutex.ReleaseMutex。

But they are different.但它们是不同的。 You may mentioned that the Mutex has a "Release", which means you may "get" something while calling "WaitOne".你可能提到互斥锁有一个“释放”,这意味着你可以在调用“WaitOne”时“得到”一些东西。 The thing you may get is related to the thread which is calling.你可能得到的东西与正在调用的线程有关。

You can call AutoResetEvent.Set in any thread.您可以在任何线程中调用 AutoResetEvent.Set。 But you can only call Mutex.ReleaseMutex from the thread which is called Mutex.WaitOne and get the true as result.但是您只能从名为 Mutex.WaitOne 的线程调用 Mutex.ReleaseMutex 并获得 true 作为结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM