UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » Some hot trick to use threads with non thread safe libs?

#1 2007-10-25 03:30 AM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Some hot trick to use threads with non thread safe libs?

Hi,

I have a code where I use threads, however in my code I use functions from a library (SSLeay) a lot of times inside the multiple threads, however the lib is not thread safe and as a result my code constantly segfault (it's unstable)...

Are there any trick to use non thread safes lib with threads in a way to it doesn't break/segfault my program?

I know the question appear very idiot, but I have no much options... without the threads the program is so slowww and i'm not skilled enought to write something like SSLeavy :)

Thank you,

Cheers

Offline

#2 2007-10-25 11:12 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Some hot trick to use threads with non thread safe libs?

The simplest solution is to add one global mutex that is taken whenever a thread-unsafe library function is called, and released when the call returns.

That way you basically serialize on the library calls, probably slowing down things quite a bit, but if the library functions don't take that long (do not block, not doing any IO), it shouldn't be too bad.

Offline

#3 2007-10-25 01:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Some hot trick to use threads with non thread safe libs?

Offline

#4 2007-10-28 01:22 AM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Some hot trick to use threads with non thread safe libs?

if ($ssl) {
   $request = new HTTP::Request GET => "https://$host/$uri";
} else {
   $request = new HTTP::Request GET => "http://$host/$uri";
}

Offline

#5 2007-10-28 01:28 AM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Some hot trick to use threads with non thread safe libs?

Offline

#6 2007-10-29 12:41 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Some hot trick to use threads with non thread safe libs?

Offline

#7 2007-12-07 12:39 AM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Some hot trick to use threads with non thread safe libs?

Hi Jeremy,

Thank you, yes, it's a simple and good idea, not sure if it will work, but i will try. :)

Thank you

Offline

  • Index
  • » Threads
  • » Some hot trick to use threads with non thread safe libs?

Board footer

Powered by FluxBB