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");
}
}
}
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");
}
}
}