what exactly is a safe point or safe place? RRS feed

  • Question

  • hi,

    Lately I have been reading Jeffrey Ritcher's great book - CLR via C#. In the book the author talks about how Thread.Abort works. Basically according to the author, when aborting a thread, CLR will try to bring the thread to a safe place. Once the thread reaches a safe place, CLR will be able to throw ThreadAbortException on that thread. A thread is in safe place if it is performing a managed blocking calls such as sleep, wait and so on. A thread is not in safe place if it is executing finally block, catch block constructor etc. However I wrote a test program where it executes an infinite loop on a thread that does not contain any bocking calls and Abort call on this thread is always able to abort it. Why? does it mean that a simple loop is also considered a safe place? How exactly does CLR determine a safe place? here is the code. 
            private static void ThreadAbortTest() 
                Thread workerThread = new Thread(new ThreadStart(Dowork)); 
                // sleep 200ms so that the worker thread 
                // can have a chance to enter the infinite loop 
                Console.WriteLine("Worker thread aborted."); 
            public static void Dowork() 
                    int a = 0
                    while (true) 

                    Console.WriteLine("end of finally block"); 
    the output is always:

    end of finally block
    Worker thread aborted.

    If I move the loop into finally block, the thread will never be aborted, but that is reasonable, because finally block is not a safe place. But how is CLR able to abort the thread when it is executing the loop in try block.

    Thanks in advance.
    Wednesday, December 17, 2008 5:48 AM


  • Couldn't find any definitive documentation on it but the SSCLI has a comment describing it as :

    "safe spot: not having any lock, not inside finally, not inside catch"

    • Marked as answer by WPFNovice_HTS Thursday, December 18, 2008 2:49 AM
    Wednesday, December 17, 2008 6:43 AM