Garbage Collector and Foreign Threads
Daniel Keep
daniel.keep.lists at gmail.com
Mon Mar 19 15:19:35 PDT 2007
will75g wrote:
> If I understand correctly how the D garbage collector works, in order to
> work properly it needs to stop every thread before performing collection.
>
> What happens if one or more of the application threads have been created
> outside the control of the D runtime? For example imagine a C/C++
> application (of which I don't even have the source code) loading a
> plug-in written by me in D... Since the D runtime knows only the threads
> created in D, the threads created by the C/C++ application could be
> still running and calling D code while the D runtime is performing a
> collection.
>
> So far the only solution I can think of is suspending the GC while my D
> code is called from the C/C++ application.
> My fear is that such a solution would lead to a lot of problems. For
> example if I have to allocate and release frequently memory, there will
> be a point where the D heap is exhausted, since the memory that I
> release won't be collected until the GC is enabled again. At that point
> every memory allocation will generate a
> std.outofmemory.OutOfMemoryException, but there's nothing I can do to
> give the collector more memory.
>
> Any idea?
In so far as I'm aware, there shouldn't be any problems with leaving the
GC enabled when your application is used as a plugin to a C app: the C
app shouldn't have any GC'ed memory (it really, really shouldn't), thus
its threads aren't a problem.
So long as the GC knows about all the threads that *do* use GC'ed
memory, you're fine.
Incidentally, the GC only (currently) runs if you try to 'new'
something, and you're out of memory. So unless the host application is
calling into your plugin from multiple threads simultaneously, I don't
think you need to worry about the GC running and then having your plugin
called in the middle of that.
Disclaimer: IANAGCE[1]
-- Daniel
[1]: I Am Not A Garbage Collector Expert :P
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d-learn
mailing list