DIP18: Non-GC threads

Michel Fortin michel.fortin at michelf.ca
Sat Sep 1 18:42:34 PDT 2012


On 2012-09-01 11:54:10 +0000, Piotr Szturmaj <bncrbme at jadamspam.pl> said:

> Michel Fortin wrote:
>>> If some of the function's arguments take references (directly or
>>> indirectly), then all of them are automatically added to the list of
>>> GC roots (addRoot()).
>> 
>> You still have a problem: you might have added an object passed as an
>> argument to the GC roots, preventing the GC from collecting it, but if
>> any thread is allowed to mutate a pointer inside this object pointing to
>> some other data, using this other data in your thread is not safe,
>> unless you have added a root for this other pointer too. You'd have to
>> recursively add roots for this to work.
>> 
>> There's also the same issue with global variables, the ones which are
>> pointers to other pointers.
> 
> Yes, there is no obvious solution, but I think it could be done. For 
> example non-immutable references may be disallowed at all.

That would be sufficient to enforce the guaranties, but it's a severe 
limitation that'll cause people to question the usefulness of the whole 
thing.

> The other solution is to copy the whole referenced object using malloc, 
> and add it to the GC roots.

Having to copy everything is also severe limitation I think. And even 
then it's not that simple to correctly copy a data structure if you 
want that to be done automatically. If you want it to be done manually, 
then how can you tell that the resulting pointer, and everything it 
points to, is not allocated from the GC heap?

-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca/



More information about the Digitalmars-d mailing list