I was wondering: Locking allows only 1 thread to enter a code region
And wait handles is for signaling : :
Signaling is when one thread waits until it receives notification from another.
So I thought to myself , can this be used to replace a lock ?
something like :
Thread number 1 --please enter ( autoreset --> autlock)
dowork...
finish work...
set signal to invite the next thread
So I wrote this :
/*1*/ static EventWaitHandle _waitHandle = new AutoResetEvent(true);
/*2*/
/*3*/ volatile int i = 0;
/*4*/ void Main()
/*5*/ {
/*6*/
/*7*/ for (int k = 0; k < 10; k++)
/*8*/ {
/*9*/ var g = i;
/*10*/ Interlocked.Increment(ref i);
/*11*/ new Thread(() = > DoWork(g)).Start();
/*12*/
/*13*/ }
/*14*/
/*15*/ Console.ReadLine();
/*16*/ }
/*17*/
/*18*/
/*19*/ void DoWork(object o)
/*20*/ {
/*21*/ _waitHandle.WaitOne();
/*22*/ Thread.Sleep(10);
/*23*/ Console.WriteLine((int) o + "Working...");
/*24*/ _waitHandle.Set();
/*25*/
/*26*/ }
as you can see : lines #21 , #24 are the replacement for the lock.
Question :
lock
, but want to know about usages scenarios)Thank you.
strange but SO does not contain a question regarding _lock vs EventWaitHandle_
AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely. If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block.
The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.
C# Lock keyword ensures that one thread is executing a piece of code at one time. The lock keyword ensures that one thread does not enter a critical section of code while another thread is in that critical section. Lock is a keyword shortcut for acquiring a lock for the piece of code for only one thread.
Do not go there. An important property of a lock is that it provides fairness. In other words, a reasonable guarantee that threads that contend for the lock get a guarantee that they can eventually acquire it. The Monitor class provides such a guarantee, implemented by a wait queue in the CLR. And Mutex and Semaphore provide such a guarantee, implemented by the operating system.
WaitHandles do not provide such a guarantee. Which is very detrimental if the lock is contended, the same thread can acquire it repeatedly and other threads can starve forever.
Use an appropriate synchronization object for locks. Wait handles should only be used for signaling.
It is possible, but it is much slower than lock() and much harder to maintain.
By the way, you should never read a value directly when using Interlocked-methods to maintain it.
Your code should look like this:
var g = Interlocked.Increment(ref i);
Then g will contain the incremented value rather than an abitrary previous value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With