Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Thread Safety Summary

I would like a summary of what exactly is thread safe in C++ both according to the current standard and C++0x as well as in practice (generally speaking, but also in my case with gcc 4.5.1).

For STL containers, my understanding is that thread safety is not guaranteed according to the current standard. Is it true though that in practice they are thread safe for single writer, multiple reader usage (on gcc and probably most modern compilers)? Is this guaranteed by C++11?.

What about POD types? I know that the standard guarantees nothing, but I've been told that in practice, all are thread safe for reading and writing. Of course even something simple like the increment operator may still need synchronization since there could be multiple reads and writes.

I'm primarily interested in the answers, but the why behind the answers would be appreciated to.

like image 298
deuberger Avatar asked Mar 21 '11 17:03

deuberger


People also ask

What is thread safety in C?

Thread safety A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.

Is C write thread-safe?

write() is certainly thread-safe. The problem is that a partial write() could require multiple calls in order to completely write the data, and while that is "thread-safe" it could result in interleaved data.

What is C threading?

A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. C does not contain any built-in support for multithreaded applications.

What is thread safety how do you achieve it?

Unlike their synchronized counterparts, concurrent collections achieve thread-safety by dividing their data into segments. In a ConcurrentHashMap, for example, several threads can acquire locks on different map segments, so multiple threads can access the Map at the same time.


1 Answers

None of the things you have mentioned are thread safe, either by the standard or in practice.

The reason that the standards do not mandate thread safety is that thread safety comes with an inherent cost. In general, C++ tries not to give you things that you don't ask for. If you want thread safety, then you have to build it yourself. This is true even in C++ 0x, which includes various synchronization primitives.

The reasons that these things are not thread safe in practice are varied. Generally, the STL containers are not thread safe because each of their basic update operations take multiple steps to accomplish. If a thread tries to read or update a container while another thread is in the process of updating it, the container will be in an indeterminate state, and thus the results will be unpredictable.

In the case of POD types, reads and writes can also take multiple steps to complete. This simplest example is a 64-bit integer on a 32-bit machine. It takes at least two instructions to either read or set the value. Once again, this means that if a thread tries to read or update the value while another thread is in the process of updating it, the results will be unpredictable.

like image 182
Peter Ruderman Avatar answered Sep 30 '22 02:09

Peter Ruderman