Garbage Collector and Foreign Threads

will75g will75g at yahoo.it
Tue Mar 20 02:19:33 PDT 2007


Sean Kelly wrote:
> Just a clarification.  By the parenthesized statement I meant that the 
> GC might be aware of the GUI thread if it is the one which initialized 
> the library because this thread may well be considered the "main" 
> thread.  After some consideration however, I don't think the GC will 
> otherwise be aware of the GUI thread *even if it is the thread which 
> triggers the collection*.  The pertinent bit of code in Phobos 1.009 is 
> internal/gc/gcx.d lines 1466 to 1500.  As you can see, the GC only 
> inspects threads in the list returned by Thread.getAll().  The GUI 
> thread won't be in that list (ignoring the comment about it possibly 
> being the "main" thread above) so its stack won't be inspected.  The GC 
> could make the calling thread a special case and obtain its thread id, 
> etc, manually, but that of breaks encapsulation for what is a pretty 
> weird situation.
> 
> It may be worth seeing if the bit about the GUI thread being the "main" 
> thread is valid.  If not, the best integrated approach would probably be 
> to generate proxy thread objects or to add some other means of unmanaged 
> threads to be suspended/inspected.  The safest approach for now, 
> however, would likely be the producer/consumer method I mentioned in my 
> prior post.

The producer/consumer method looks like a good solution and is not too 
far from the design I'm currently using in C++.

I still would prefer a generic solution for these situations. My plug-in 
is a special case, but if D doesn't have a mean to interact with foreign 
threads it will be a big limitation for any DLL made in D and meant to 
be used from languages other than D.

If I understand correctly being able to block the foreign thread is not 
enough, because the GC must also be able to inspect the thread stack. If 
that's the case, your proxy thread idea is probably the cleanest 
solution. I'll see if I can manage to implement it (having something 
like that in Tango would be fantastic).

Thanks for your help.


More information about the Digitalmars-d-learn mailing list