views:

100

answers:

3

I am not sure how lock works.
What happens if I have a List<T> list and 2 threads ?
What happens if thread1 start running first and enumerate that list

foreach(T t in list)
{
  // code
}

and in the same time, but after thread1 has started, thread2 will lock the list

lock(list)
{ 
  // code
}

I use ThreadPool do to some processing and I need to know how lock really works and if is thread safe

ThreadPool.QueueUserWorkItem(new WaitCallback(method), obj);
+5  A: 

lock keyword doesn't "lock" or "freeze" target object (in a sense preventing from changes).

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

So in your case it won't prevent other threads from enumerating the list.

Dzmitry Huba
+5  A: 

In the code as written - the enumerator will keep on going. The point about lock is that all your code needs to agree. If you had used:

lock(list)
{
    foreach(T t in list)
    {
        // code
    }
}

Then when your other thread attempts to acquire the lock it will queue behind the first - waiting for the first thread to release the lock (either by exiting the lock (Monitor.Exit), or calling Monitor.Wait).

Marc Gravell
+1  A: 

Lock isn't magic, and it must be used cooperatively. If I want to ensure that changes to some mutable object don't get accidentally stomped on or corrupted by multiple threads then I need to ensure that I only make changes to the object within a lock block.

The only thing that lock ensures is that any other code that is locking on the same object is not running on other threads at the same time, it's just syntactic sugar for acquiring and releasing a mutex.

lock(x) // acquire mutex associated with x, or wait until it becomes available
{
    // do stuff
} // release mutex associated with x

Edit: MSDN has very worthwhile under-the-hood details on lock(), for those who are curious.

Wedge
While it is a *logical* mutex, note that this (`Monitor`) is very different to the OS-provided mutex, exposed as `Mutex` in .NET.
Marc Gravell
@Marc, yes, that's true, under the hood lock() uses a Monitor, which uses a (little-m) mutex, but this shouldn't be confused with the (big-m) Mutex class which is usually used for cross-process synchronization.
Wedge