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

Kapps opantm2+spam at gmail.com
Sat Jan 12 16:35:51 PST 2013


On Saturday, 12 January 2013 at 16:41:38 UTC, Robert M. Münch 
wrote:
> Hi, I have a C library that uses multi-threading and implements 
> a callback architecture by this. It runs on Windows and *nixes 
> (pthreads). I would like to use it from D.
>
> I understand that the ABI of D is C compatible, so I would 
> expect that there shouldn't be any problems with threading as 
> well. Can I somehow use D's threading features in combination 
> with the libraries C features? Or is it better to completely 
> keep the two seperate?
>
> Has anyone any experience in mixing D and C in multi-threaded 
> envrionments?
>
> Thanks for the feedback.

If you're using a thread created from a C library, and it uses 
the GC, you'll need to call thread_attachThis 
(http://dlang.org/phobos/core_thread.html#.thread_attachThis) on 
each thread that uses it so that the GC knows to suspend the 
thread during a collection. If you don't do this, you'll get 
random segfaults and other issues.


More information about the Digitalmars-d mailing list