Writing a thread-safe library without pulling in pthreads

Ian Lance Taylor iant@google.com
Fri Jul 18 20:41:00 GMT 2008


Florian Weimer <fw@deneb.enyo.de> writes:

> * Ian Lance Taylor:
>
>> Florian Weimer <fw@deneb.enyo.de> writes:
>>
>>> I'd like to write a library which only uses locking if the application
>>> is linked against pthreads.  Is this possible?
>>
>> Yes, and in fact libsupc++ in gcc does it.  You can declare the
>> pthread functions as weak references.  Then you only call them if the
>> address of the function is not NULL.
>
> Thanks.
>
> But I'm hosed if a library is loaded later (via dlopen) that pulls in
> threads, right?  This looks unsupported, so it should be okay if I can't
> deal with this situation.

Yes, you are probably hosed in that case.  Offhand I can't think of
any reasonable way to make that work correctly, given that the dlopen
might occur when a lock would nominally be held.

Ian



More information about the Gcc-help mailing list