Whats is the difference between AutoResetEvent and Mutex
Asked Answered
S

3

15

I am new to these concepts. But as i am going deeper in threading i am getting confused.

What is the significance of mutex, semaphore over autoresetevent.

Only difference i came to know with studies is that a mutex can perform across process operations. If this is the case why it does not have same method as Set, Reset, WaitOne.

Can we replace the AutoResetEvent with mutex and vice versa?

Please solve this puzzle.

Skindive answered 11/1, 2012 at 6:27 Comment(1)
Please read albahari.com/threadingPogonia
J
27

Different concept - a Mutex is an exclusive token; 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. When they pass through the gate immediately closes.

Jard answered 11/1, 2012 at 6:31 Comment(4)
Yes, but both solve the same purpose. At one point of time one only person has keys.Skindive
@DJ no, they don't. Nobody "has keys" for an AutoResetEvent; you queue, and then when the gate opens you pass through. You don't get a region of time when you hold a key.Jard
But if i take ManualResetEvent. I think then we have the keys. So Mutex can be replaced with ManualResetEvent?Skindive
@DJ no it can't. The point of a mutex is to take the mutex for a duration of time, for example to do a few changes that need to be considered as a single unit. While we have it, we know that nobody else does. This is not the case for an auto-reset event; the "open" button can be pushed as many times as you like, by any code. There is no "I'm done, back it goes", unless your "doing" code is pressing the "open" button on the way out, and no other code is. But that is not a common use of an auto-reset-event.Jard
C
4

It depends.

In common, AutoResetEvent and Mutex can be replaced, AutoResetEvent.WaitOne = Mutex.WaitOne and AutoResetEvent.Set = Mutex.ReleaseMutex.

But they are different. You may mentioned that the Mutex has a "ReleaseMutex", which means you may "get" something while calling "WaitOne". The thing you may get is related to the thread which is calling.

You can call AutoResetEvent.Set in any thread. But you can only call Mutex.ReleaseMutex from the thread which is called Mutex.WaitOne and get the true as result.

Collettecolletti answered 24/7, 2019 at 6:40 Comment(0)
A
0

Mutex is blocking the threads to access the Critical section; Here In AutoResetEvent, I see the focus is not on blocking the critical section rather on the Signal it should receive from any other thread. Once it is signalled, it is allowed to continue the execution.

AutoResetEvent also provide an option to handle race condition, lets say set() event is called first then some thread calls wait() on it then wait() immediately receives the signal given by set() and anyway vice versa also works fine.

Also If multiple set() are called before any wait() on it and then wait() arrives multiple set() will continue to be valid, Only one set() can be waiting for wait() and remaining would vanish immediately.

Avitzur answered 1/9, 2021 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.