Foreign threads in D code.

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 12 02:10:07 PDT 2017


On 2017-07-11 08:18, Biotronic wrote:

> If DRuntime is not made aware of the thread's existence, the thread will 
> not be stopped by the GC, and the GC might collect memory that the 
> thread is referencing on the stack or in non-GC memory.

Are you sure? Wouldn't that make malloc or any other custom allocators 
completely useless and the D GC would completely break the C standard 
library because it could collect memory allocated by the C standard library?

 From "How Garbage Collection Works":

"5. Freeing all **GC** allocated memory that has no active pointers to 
it and do not need destructors to run" [1]. I added the emphasize on "GC".

 From "Interfacing Garbage Collected Objects With Foreign Code"

"If the only pointer to an object is held outside of these areas, then 
the collector will miss it and free the memory. To avoid this from 
happening, either
* reallocate and copy the object using the foreign code's storage 
allocator or using the C runtime library's malloc/free." [2].

[1] http://dlang.org/spec/garbage.html#how_gc_works
[2] http://dlang.org/spec/garbage.html#gc_foreign_obj

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list