Blocking the current thread: is WTF possible?

Studying the old code, we came across the following code:

lock (System.Threading.Thread.CurrentThread) // Critical Section - no interrupting.

Our first reaction was “WTF” - the code blocks the current thread, which is a different object for each thread (except in the case of recursion, which would just be silly to block, then). This castle seems to serve no purpose. We were wondering if this is really a WTF, or whether it really serves any purpose.

+5
source share
5 answers

WTF , - , . .

+4

, Thread.Abort . , , (, , , ), , . , - (, , ); , , , . , . , , (Thread.Abort, , ).

+2

, , , . , . threadpool?

, :)

+1

, CPU .

lock() , , , . , , , , .

object someObj = new object();
lock (someObj) {
    lock (someObj) {
        // this code will execute even though it had to go through two locks on the same obj
    }
}
+1

, . , , .

,

this

, API,

Thread.CurrentThread

- , .

The converse is also true if you have a thread safety object, avoid locking the object itself (since it may be locked inside 'this'), use the highlighted object instead. If you need to synchronize from outside the class, the selected object can be made public. An example of this is

ICollection.SyncRoot
0
source

All Articles