Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Locking an object that is reassigned in lock block

I have this code in a class:

private static MyObject _locker = new MyObject();

...

lock (_locker)
{
     ...
     _locker = new MyObject();
     ...
}

Will it keep the lock on _locker ?

like image 286
Theodore Zographos Avatar asked Jun 02 '10 11:06

Theodore Zographos


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

No, it will not. From the C# specification (emphasis is mine):

A lock statement of the form lock (x) ... where x is an expression of a reference-type, is precisely equivalent to

System.Threading.Monitor.Enter(x);
try {
  ...
}
finally {
  System.Threading.Monitor.Exit(x);
}

except that x is only evaluated once.

Since x is not reevaluated the lock will be released.

like image 68
João Angelo Avatar answered Sep 27 '22 21:09

João Angelo


I assume it will keep the lock on the MyObject instance that was set to _locker when lock has been called, i.e. it will keep the lock on the original instance of _locker, not on the newly created MyObject instance. In the following code, the lock will be kept on MyObject("OriginalInstance") when the lock is called for the first time. When it is called the second time, it will lock on MyObject("NewInstance").

private static MyObject _locker = new MyObject("OriginalInstance");

...

lock (_locker)
{
    ...
    _locker = new MyObject("NewInstance");
    ...
}

Therefore, the next thread may enter the critical section without problems because the new instance is not locked.

Anyway, doing stuff like this is generally considered bad practice. See MSDN for some advices on how to use lock.

like image 41
gehho Avatar answered Sep 27 '22 22:09

gehho


Don't do this. Consider using a separate object entirely to hold the state of the lock, not necessarily the object you want to protect in the lock statement. I often write code (ok, not often) like this:

private static readonly object _locker = new object();
private static MyObject _object;
...

lock (_locker)
{
     ...
     _object = new MyObject();
     ...
}

It involves a completely different sort of program flow than what you're looking at. lock() defines a critical section in the code -- you don't use it as an all-purpose thread safety mechanism for any type of object (which is what I think your intent is in your code?)

like image 33
Dave Markle Avatar answered Sep 27 '22 22:09

Dave Markle