Foreign threads in D code.
Guillaume Piolat via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 12 02:49:32 PDT 2017
On Tuesday, 11 July 2017 at 22:59:42 UTC, Igor Shirkalin wrote:
> On Tuesday, 11 July 2017 at 06:18:44 UTC, Biotronic wrote:
>> On Monday, 10 July 2017 at 20:03:32 UTC, Igor Shirkalin 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. 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
>
> Thanks for very useful information!
Just one small note.
If you don't know the foreign thread lifetime, it's cleaner to
detach it from the runtime upon exit.
Else may fall in the following scenario.
1. you register thread A
2. thread A is destroyed later on, in the C++ code
3. another thread B come into your callback and allocate. The GC
triggers and try to pause a non-existing thread A.
More information about the Digitalmars-d-learn
mailing list