Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a lock statement do under the hood?

I see that for using objects which are not thread safe we wrap the code with a lock like this:

private static readonly Object obj = new Object();  lock (obj) {     // thread unsafe code } 

So, what happens when multiple threads access the same code (let's assume that it is running in a ASP.NET web application). Are they queued? If so how long will they wait?

What is the performance impact because of using locks?

like image 303
NLV Avatar asked May 17 '11 10:05

NLV


People also ask

What is lock statement?

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.

What does a threading lock do?

A lock allows you to force multiple threads to access a resource one at a time, rather than all of them trying to access the resource simultaneously.

Why should you avoid the lock keyword?

Avoid using 'lock keyword' on string object String object: Avoid using lock statements on string objects, because the interned strings are essentially global in nature and may be blocked by other threads without your knowledge, which can cause a deadlock.

Why do we lock objects?

Lock Object is a feature offered by ABAP Dictionary that is used to synchronize access to the same data by more than one program. Data records are accessed with the help of specific programs. Lock objects are used in SAP to avoid the inconsistency when data is inserted into or changed in the database.


2 Answers

The lock statement is translated by C# 3.0 to the following:

var temp = obj;  Monitor.Enter(temp);  try {     // body } finally {     Monitor.Exit(temp); } 

In C# 4.0 this has changed and it is now generated as follows:

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

You can find more info about what Monitor.Enter does here. To quote MSDN:

Use Enter to acquire the Monitor on the object passed as the parameter. If another thread has executed an Enter on the object but has not yet executed the corresponding Exit, the current thread will block until the other thread releases the object. It is legal for the same thread to invoke Enter more than once without it blocking; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock.

The Monitor.Enter method will wait infinitely; it will not time out.

like image 93
Steven Avatar answered Oct 06 '22 18:10

Steven


Its simpler than you think-

According to Microsoft: The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

The lock keyword calls Enter at the start of the block and Exit at the end of the block. lock keyword actually handles Monitor class at back end.

For example:

private static readonly Object obj = new Object();  lock (obj) {     // critical section } 

In the above code, first the thread enters a critical section, and then it will lock obj. When another thread tries to enter, it will also try to lock obj, which is already locked by the first thread. Second thread will have to wait for the first thread to release obj. When the first thread leaves, then another thread will lock obj and will enter the critical section.

like image 22
Umar Abbas Avatar answered Oct 06 '22 18:10

Umar Abbas