Thread Safety
In this day of multi-core CPUs and parallel programming platforms a common meme amongst developers has become “Is library X thread safe?”. As reasonable as that sounds, it’s actually not a particularly interesting question to ask, and anyone who claims to have a useful answer, probably doesn’t know what’s going on. I say that because unless you say what you want to do in parallel, it’s meaningless to say whether or not a library is thread safe.
Take a popular definition of thread safety, this one’s from wikipedia:
A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.
Using that definition, is libevent thread-safe? The answer turns out to be a little more complicated than a yes or no answer, it depends what you’re trying to do in each of those threads.
What about Rails ? Well, strictly speaking Rails is currently thread-safe. It functions correctly during simultaneous execution, we just have this huge mutex around the entire request so we only handle one request at a time. What about Active Record? Well, yes… But it opens a connection per thread which will quickly exhaust your database server’s resources. Both of these restrictions present problems for people wanting to use rails in a highly threaded environment, so perhaps thread safety isn’t a particularly useful goal without further clarification.
It’s just not useful to say whether or not rails ( or any other librarary ) is ‘thread safe’ without saying what you want to do in parallel, the code may function correctly but run slower than it would in a single thread.
With rails there are several different concurrent use cases we could aim to support in our new thread-safety project
- Dispatch requests in parallel with a single thread per request
- Allow rendering to be performed in parallel for a single request (several threads each rendering a seperate partial for the same response)
- Make Active Record use a connection pool to prevent opening an excessive number of connections
- Allow a single Active Record object to be worked with concurrently in multiple threads.
Each of these goals will involved differing numbers of locks, and differing degrees of change to the current design. They’ll also improve (or degrade) performance by different factors for different benchmarks. Whatever the results of this process there will still be some locks around some methods, and those locks are bound to annoy someone.
Phrases like “W is completely threadsafe” or “X isn’t thread safe” should be a red flag to you. They’re a sign that the speaker doesn’t quite understand the subtlety of the question, and the irrelevance of the answer without further qualification.

James Bennett May 4th, 2008 @ 09:11 PM
Amen.
bryanl May 5th, 2008 @ 12:05 AM
So, in summary, Rails is fine just the way it is?
John Levon May 5th, 2008 @ 04:22 AM
I don’t agree that it’s a subtle question, it’s a simple one. libevent isn’t thread-safe. Some of its functions are with qualifications. It’s also never irrelevant, although often more information is certainly needed.
Of course, the right thing is to carefully document the status of each function and, where necessary, their interactions. This would include some detail on stuff like big locks (which is surely not a safety thing, so I’m not sure why you connect it?).
Koz May 5th, 2008 @ 08:56 AM
@bryanl: I’m not sure if you read the whole thing, but we have a summer of code project in place for investigating all this stuff. I know it’s popular to pretend we don’t care about this issue, but it’s nonsense.
@john: for a given method or function thread safety is a simple question. Either it breaks or it doesn’t. But for an entire library it’s a much more complicated and your usage is important.
Peter Lawrey May 5th, 2008 @ 09:06 PM
A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.
If “it depends what you’re trying to do in each of those threads.” then its not thread safe. Otherwise you could say all code was thread-safe. Efficiency is not a thread-safety issue. While good multi-threaded code needs to take into account many considerations, thread safety is just about “is it safe”
Geoffrey Wiseman May 6th, 2008 @ 01:54 PM
Some libraries are likely to fall over heard if used with multiple threads, others will work at some base level. Yes, the use of the library and how it handles threads is likely to be important to someone who’s planning on using it with multiple threads, but those factors are unimportant if the library isn’t threadsafe at all.
I guess I’m with John: knowing a library is threadsafe is a necessary if insufficient first step in investigating its use in a multithreaded context.
Michael Schuerig May 6th, 2008 @ 09:06 PM
I think it is reentrance:http://en.wikipedia.org/wiki/Reentrant you’re after, not thread-safety.
Code with a giant lock around it is trivially thread-safe.