Question On Multithreading Lock
-
Within the multithreading enviroments, there is an exclusive lock provided by the .Net, which will block all other threads trying to enter the critical section if currently there is a threading holding this critical section. But I'm trying to find a unblocking lock whick is exclusive but won't block other threads, just let them return and inform them the critical section is not available for them to access. Anyone knows if there is this kind of lock? Thanks a lot;)
-
Within the multithreading enviroments, there is an exclusive lock provided by the .Net, which will block all other threads trying to enter the critical section if currently there is a threading holding this critical section. But I'm trying to find a unblocking lock whick is exclusive but won't block other threads, just let them return and inform them the critical section is not available for them to access. Anyone knows if there is this kind of lock? Thanks a lot;)
lock (obj) {
//...
}is just a syntax sugar for:
System.Threading.Monitor.Enter(obj);
try {
//...
} finally {
System.Threading.Monitor.Exit(obj);
}You should take a look at the other Monitor methods, there's a method called "TryEnter".
-
lock (obj) {
//...
}is just a syntax sugar for:
System.Threading.Monitor.Enter(obj);
try {
//...
} finally {
System.Threading.Monitor.Exit(obj);
}You should take a look at the other Monitor methods, there's a method called "TryEnter".