A safe point is a place in the managed code where
it is safe to allow garbage collection. For instance, if the CLR determines it is time to perform
garbage collection, it must suspend all threads temporarily while it performs the collection.
When the garbage collector (GC) suspends threads for collection, it must wait until they
all have reached a safe point where it is okay to move things around on the heap. The call
to Suspend() allows the thread to reach a safe point before actually suspending it. Never use
Suspend() and Resume() to orchestrate thread synchronization. The fact that the system allows
the thread to continue running until it reaches a safe point is a good enough reason not to rely
on this mechanism.
Waiting for a Thread to Exit
In this chapter??™s previous examples, we??™ve used the Join method to wait for a specific thread to
exit. The name of the method is suggestive of the fact that you??™re joining the current thread??™s
execution path to that of the thread you??™re calling Join() on, and you cannot proceed until
your joined thread arrives.
Naturally, you??™ll want to avoid calling Join() on the current thread. The effect is similar to
calling Suspend() from the current thread. The thread is blocked until it is interrupted.
Pages:
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453