I have to use a library which doesn't support multithreads.
Is it possible/enough to use this lib in a multithread context by
using a global mutex that allows only one thread to call the lib at a
time ?
thanks
It's not safe in general, but depending on what the library actually
does it could be safe in your particular scenario.
One problem that may arise is if the library uses something like
gethostbyname(), which points to a static data structure. None of your
threads should be using it directly, but if you had another library
(protected by a different mutex) a second thread could end up calling
gethostbyname() and overwriting the results of the call in the first
thread. In this case, protecting both libraries with the same mutex
would solve the problem.
Another possible source of problems is if the library does anything
related to signal handling. It could conceivably expect signals to
arrive in that particular thread, and this could potentially cause problems.
Chris
Actually it's an openGL lib which is know to be not thread safe.
>
> One problem that may arise is if the library uses something like
> gethostbyname(), which points to a static data structure. None of your
> threads should be using it directly, but if you had another library
> (protected by a different mutex) a second thread could end up calling
> gethostbyname() and overwriting the results of the call in the first
> thread. In this case, protecting both libraries with the same mutex
> would solve the problem.
hmm that could happen if I use more than one not thread safe library.
But it's not my case.
>
> Another possible source of problems is if the library does anything
> related to signal handling. It could conceivably expect signals to
> arrive in that particular thread, and this could potentially cause problems.
>
Indeed.
So that basically means that I cannot use a not thread safe lib even
if I use a global mutex to synchronise accesses to this lib since I
have no idea how this lib is implemented.
> So that basically means that I cannot use a not thread safe lib even
> if I use a global mutex to synchronise accesses to this lib since I
> have no idea how this lib is implemented.
It means you aren't guaranteed that it is safe. It may actually work,
but unless you look at the implementation you won't know for sure.
Chris
You could use ldd or whatever your OS has to see
which unsafe functions your unsafe library uses, then
put a mutex around anything else that uses them
as well.
Sean
Solaris does support this, providing the non-thread-safe library(s)
are only called from the main/initial thread. Since they can only be
called from one thread, no global mutex is required.
If you call them from other threads, the sort of problem they'll
have is that they will be seeing/using the wrong thread's errno
variable.
Other OS's won't necessarily handle things the same way though.
--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]