Is it possible to use a C multi-threaded lib with D (multi-threading)?

Stewart Gordon smjg_1998 at yahoo.com
Sun Jan 13 13:12:43 PST 2013


Oops ... seem to have accidentally sent an email reply before.  Let's 
try again....

On 13/01/2013 14:46, Robert M. Münch wrote:
<snip>
> I have it the other way: D main program, which starts C threads. And
> yes the C code is thread safe.

You mentioned callbacks in your original post.  Does this mean that the 
C code may call D functions from the threads it creates?  And do said D 
functions use the GC either directly or indirectly?  If so, you will 
need to use attachThis at the beginning of each callback, and then 
detachThis when you're done.  (But you might be able to get away with 
less than this, depending on how the library uses threads and invokes 
callbacks from them.)

I haven't tried it, as I haven't had reason to use a C multi-threaded 
library in D code.  But going by the documentation, what attachThis does 
is to create a D thread object for the thread created by the C library, 
so that
(a) the GC will pause the thread in order to perform GC
(b) any pointers to GC memory held by the thread will be followed

OTOH, detachThis releases the thread from the GC, thereby making it safe 
for the C library to destroy the thread after it's finished with it.

Stewart.


More information about the Digitalmars-d mailing list