Threading question

C

Can Balioglu

What happens if I set the reference to null for an object that is used for
mutual exclusion? For example I'm using the _sync object for controlling
thread synchronization but in one of my critical sections I'm setting it to
null. What happens to the other lock statements that are waiting for the
_sync object? I wrote the following code but I didn't understand how the
runtime behaves in such a case. Thanks for replies.

class Class1 {
private static Object _sync = new Object();

[STAThread]
static void Main(String[] args) {
Thread thread1 = new Thread(new ThreadStart(Thread1));

thread1.Start();

lock(_sync) {
//_sync = null; --> Causes 'Thread1' to throw an
ArgumentNullException.

Thread.Sleep(new TimeSpan(0, 0, 1));

_sync == null; // this works even if it's null.
}

Console.ReadLine();
}

static void Thread1() {
lock(_sync) {
Console.WriteLine("Thread1");
}
}
}
 
J

Jon Skeet [C# MVP]

Can Balioglu said:
What happens if I set the reference to null for an object that is used for
mutual exclusion? For example I'm using the _sync object for controlling
thread synchronization but in one of my critical sections I'm setting it to
null. What happens to the other lock statements that are waiting for the
_sync object? I wrote the following code but I didn't understand how the
runtime behaves in such a case. Thanks for replies.

lock(foo) ends up as

object tmp = foo;
Monitor.Enter(tmp);
try
{
....
}
finally
{
Monitor.Exit(tmp);
}

So whatever you enter the lock with is what you leave it with, even if
the value of the expression itself has changed during that time.

Monitor.Enter(null) will always throw an exception.

It's basically a bad idea to change a synchronization reference though
- why do you want to do it?
 
J

John Wood

in one of my critical sections I'm setting it to null. What happens to
the other lock statements that are waiting for the _sync object? <<

You're setting the variable holding a reference to the sync object to null,
you're not disposing of the instance itself, so that shouldn't affect the
synchronization statements that are already running because they're just
monitoring the content of the variable when the statement was executed (ie.
the instance).
 
W

William Stacey [MVP]

why do you need to set sync to null ? You should make this readonly so you
set it once in construction.
 
C

Can Balioglu

My problem was a collection's SyncRoot property. Some of the threads were
using it for synchronized access. But there was an event which could set the
collection to a null reference. I used a private field instead of the
SyncRoot for the synchronization and that solved my problem. I asked it to
be sure how the runtime behaves in such a scenario. Thanks a lot for your
reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top