Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long will a C# lock wait, and what if the code crashes during the lock?

Tags:

c#

locking

I saw the following code, and wanted to use it for a simple activity which may only be executed one at a time, and won't occur frequently (so the chance of occurring twice at a time is very small, but you never know).

So the code:

// class variable private static object syncRoot = new object();  // in a method: lock (syncRoot) {     DoIt(); } 

When another thread comes by and wants to execute the code, how long will it wait until the lock is released? Forever, or can you somehow set a timeout?

And second: if the DoIt() method throws an exception, is the lock still released?

like image 477
Michel Avatar asked May 18 '11 18:05

Michel


People also ask

How long does it take AC to go down 1 degree?

It should take a standard AC 18 minutes to cool down a house by 1 degree. It should take a standard AC 36 minutes to cool down a house by 2 degrees. It should take a standard AC 54 minutes to cool down a house by 3 degrees.

How long AC should run on hot day?

Most of the Time, Aim for A 15 Minute Cycle Ideally, your AC should run for 15 minutes on nice, warm days. Only on those rare triple-digit temperature days should your air conditioner be running almost all the time. If your AC isn't following this pattern, you could have a problem.

Can an AC last 30 years?

Air conditioners can last 10-15 years depending on various factors, while HVAC systems can even go up to 30 years before needing to be replaced.

How long should AC Stay off between cycles?

Quality of your Air Conditioning Unit So, how long should AC stay off between cycles? Between 7 and 10 minutes after every cycle is ideal. The trick is to avoid very long or short cycles.


2 Answers

When another thread comes by and wants to execute the code, how long will it wait until the lock is released?

lock will block the the thread trying to enter the lock indefinitely until the object being locked on is released.

can you somehow set a timeout?

If you need to specify a timeout, use Monitor.TryEnter as in

if(Monitor.TryEnter(obj, new TimeSpan(0, 0, 1))) {     try {         body      }     finally {         Monitor.Exit(obj);     } } 

if the DoIt() method throws an exception, is the lock still released?

Yes, a lock(obj) { body } is translated to:

bool lockWasTaken = false; var temp = obj; try { Monitor.Enter(temp, ref lockWasTaken); { body } } finally { if (lockWasTaken) Monitor.Exit(temp); } 

For the gory details on what can happen when an exception is thrown, see Locks and exceptions do not mix.

like image 70
jason Avatar answered Sep 27 '22 00:09

jason


As mentioned, a regular lock will wait forever, which is a risk of deadlocks.

The preferred mechanism is (and note the ref):

bool lockTaken = false; try {     Monitor.TryEnter(lockObj, timeout, ref lockTaken);     if(!lockTaken) throw new TimeoutException(); // or compensate     // work here... } finally {     if(lockTaken) Monitor.Exit(lockObj); } 

This avoids the risk of not releasing the lock in some edge-cases.

The finally (which exists in any sensible implementation) ensures the lock is released even in error conditions.

like image 31
Marc Gravell Avatar answered Sep 27 '22 00:09

Marc Gravell