Foreign threads in D code.

Biotronic via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 10 23:18:44 PDT 2017


On Monday, 10 July 2017 at 20:03:32 UTC, Igor Shirkalin wrote:
> Hello!
>
> I have written some D code that I need to link to :C++ huge 
> project. Let it be just one function that uses GC. The question 
> is: if C++ code creates several threads and runs this :D 
> function simultaneously, will GC work correctly?
>
> p.s. Of course the druntime is initialized before it.
>
> Igor Shirkalin

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. Anything allocated by the GC would still be scanned.

To inform DRuntime about your thread, you should call 
thread_attachThis:

https://dlang.org/phobos/core_thread.html#.thread_attachThis

As pointed out in the documentation of thread_attachThis, you 
might also want to call rt_moduleTlsCtor, to run thread local 
static constructors. Depending on your usage, this might not be 
necessary.

--
   Biotronic


More information about the Digitalmars-d-learn mailing list