3
votes

I stumbled upon the thread safety article on Wikipedia; it distinguishes several levels of safety, especially:

Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.
Conditionally safe: Different threads can access different objects simultaneously, and access to shared data is protected from race conditions.

But to me, both definitions look like different ways to say the same thing. Both guarantee there is no race condition on shared data.

Could someone explain the difference? Thanks.

1
I think "Conditionally Safe" just means that, because the threads are accessing different objects which have separate data, there's no possibility of a threading problem. - Matthew Watson

1 Answers

3
votes

You should understand that shared data is not the same thing in the two cases.

Thread safe talks about accessing a single instance from multiple threads. So shared data may be any member of that class, if accessed by public methods. It's not shared between instances (because there is only one) but only between threads.

Conditionally safe talks about accessing different instances, each from its own thread. Data must be shared between instances, so it can be only an aggregated member (perhaps provided by dependency injection), static member or a (external) singleton.

But, if you read all the citations in the mentioned Wikipedia article (the Qt one is wrong), you will understand that Wikipedia might have even misrepresented the IBM naming. Conditional by IBM means that only some methods from the class/API are threadsafe, or that the threadsafety depends on underlying services which the API can't influence (giving a fine example). The Qt naming convention of threadsafe vs. reentrant seems to be more appropriate, as it doesn't distinguish between thread shared data and instance shared data.