Confusion about the lock statement in C# Confusion about the lock statement in C# multithreading multithreading

Confusion about the lock statement in C#


The question is confusingly worded and the answers so far are not particularly clear either. Let me rephrase the question into several questions:

(1) Does the lock statement ensure that no more than one thread is in the body of the lock statement at any one time?

No. For example:

static readonly object lock1 = new object();static readonly object lock2 = new object();static int counter = 0;static object M(){    int c = Interlocked.Increment(ref counter);    return c % 2 == 0 ? lock1 : lock2;}...lock(M()) { Critical(); }

It is possible for two threads to both be in the body of the lock statement at the same time, because the lock statement locks on two different objects. Thread Alpha can call M() and get lock1, and then thread Beta can call M() and get lock2.

(2) Assuming that my lock statement always locks on the same object, does a lock statement ensure that no more than one "active" thread is in the body of the lock at any one time?

Yes. If you have:

static readonly object lock1 = new object();...lock(lock1) { Critical(); }

then thread Alpha can take the lock, and thread Beta will block until the lock is available before entering the lock body.

(3) Assuming that I have two lock statements, and both lock statements lock on the same object every time, does a lock statement ensure that no more than one "active" thread is in the body of either lock at any one time?

Yes. If you have:

static readonly object lock1 = new object();...static void X() {    lock(lock1) { CriticalX(); }}static void Y() {    lock(lock1) { CriticalY(); }}

then if thread Alpha is in X and takes the lock, and thread Beta is in Y, then thread Beta will block until the lock is available before entering the lock body.

(4) Why are you putting "active" in "scare quotes"?

To call attention to the fact that it is possible for a waiting thread to be in the lock body. You can use the Monitor.Wait method to "pause" a thread that is in a lock body, and allow a blocked thread to become active and enter that lock body (or a different lock body that locks the same object). The waiting thread will stay in its "waiting" state until pulsed. At some time after it is pulsed, it rejoins the "ready" queue and blocks until there is no "active" thread in the lock. It then resumes at the point where it left off.


You put a lock on an object. If another thread tries to access a critical section marked by that object at the same time, it will block until the lock is removed/complete.

Example:

public static object DatabaseLck= new object();lock (DatabaseLck) {        results = db.Query<T>(query).ToList();     }

Or

lock (DatabaseLck) {       results = db.Query<T>(string.Format(query, args)).ToList();  }

Neither one of those code blocks can be run at the same time BECAUSE they use the same lock object. If you used a different lock object for each, they could run at the same time.


It is one and the same critical section.

lock (synclock){  // the critical section protected by the lock statement  // Only one thread can access this at any one time}

See lock Statement on MSDN:

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock.


Or does it mean: The lock keyword ensures that one thread does not enter any critical section of code while another thread is in any critical section. ?

No. It does not mean that. It means the critical section protected by that lock and that lock alone.


Update, following code example:

If you use a single object to lock on, it will lock all critical sections, causing other threads to block until released. In your code example, once the lock in MethodA has been entered, all other threads reaching that lock and the lock on MethodB will block until the lock is released (this is happening because you are locking on the same object in both methods).