www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Attaching and detaching threads from a C callee.

Hi,

I've asked this question before on the learn mailinglist but 
haven't
received any answer yet so I think this might belong to general.

I am looking for a convenient way to detach all but the current 
thread
from the druntime. I know thread_detachByAddr and 
thread_detachThis
exists, however I need to do this from the main thread and Thread
doesn't expose addr which can be used for detachByAddr.

In detail:
The libfuse library is one of the rare occassions of library in 
which
the caller has to hand all control to the library's main entrance
function fuse_main. The library itself will create pthreads
to handle multiple synchronous requests. The library doesn't 
offer any
way to hook into the creation or joining of threads, however the
destroy/init functions will only be called on the main thread
before the threading is started and just before threads are
joined. Attaching threads is fine as you can just attach every 
time you
get called from fuse. Detaching however isn't. The destroy 
operation get's only called
on one thread and therefore there is no reliable way of detaching.

This leads the garbage collector to crash on a final sweep after 
control
from the C library is returned, as it tries to suspend 
non-existant
thread.

I am aware that this is a very unique case and I am wondering 
what the
best approach to solve this is. My current approach is to simply 
exit
the program on destroy() and therefore causing termination of the
program with all threads enabled.

Does it make sense at all to maybe expose Thread.addr in 
core.memory or am I just missing something.

- David
May 23 2014