views:

192

answers:

1

It’s fairly well documented that when .NET's automatic garbage collector runs, it will temporarily pause all running managed threads associated with the application domain. What I haven't been able to discover are details on what happens to native threads created by the application when garbage collection occurs (ie. using _beginthreadex() instead of System.Threading.Thread()). Are they similarly paused or are the left running?

+8  A: 

Does this help?

"A GC won't stop threads that are not running managed code. Since those threads can't be touching the GC's heap anyways, there's no need for the GC to coordinate with them."

"If a thread was in managed code but called out to native code, it will continue to run. It will be stopped if it returns back to managed code."

Jason Evans
+1 Nice link. Very interesting stuff. Hope you don't mind, I edited your post to include the relevant bit from the blog in case it goes off line.
Simon P Stevens
That's interesting. Why can't those threads be touching GC heap? What about `gcroot<T>` and friends?
Pavel Minaev
As soon as a native thread use managed code, it is registered with the garbage collector. So when the GC runs, the thread is suspended. It is only when the thread dies that it gets unregistered.
Laurent Etiemble