Are there any cpp functions or objects (excluding inherited from c) that are not thread safe even when each thread operates on its own data?

StackOverflow https://stackoverflow.com/questions/7471540

  •  22-01-2021
  •  | 
  •  

Question

Sorry for the long title, but I think it explains well what I'm interested in. For example C function strtok is not thread safe in worst possible way :) , it uses a global state. So even if it is called on different data it is not thread safe. So my question is are there functions in "C++ minus C" that have same problem. Again I'm not interested in things like "if you write to same file from 10 threads it is undefined behavior". What I'm interested is "if you write to 2 diff files from 2 diff threads (each thread writes to its own file )that is not thread safe."

Was it helpful?

Solution

Thread safety is only really covered by C++11; C++03 didn't specify multi-threaded behavior.

In C++11, the relevant bits are 1.10/8 "Certain library calls synchronize with other library calls performed by another thread. For example, an atomic store-release synchronizes with a load-acquire that takes its value from the store (29.3)." and especially §17.6.5.9 Data race avoidance.

The cases you mention are obviously disallowed: "

  1. This section specifies requirements that implementations shall meet to prevent data races (1.10). Every standard library function shall meet each requirement unless otherwise specified. Implementations may prevent data races in cases other than those specified below.
  2. A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s arguments, including this.
  3. A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s nonconst arguments, including this.
  4. [ Note: This means, for example, that implementations can’t use a static object for internal purposes without synchronization because it could cause a data race even in programs that do not explicitly share objects between threads. —end note ]

Where the text above says "unless otherwise specified", it includes some C function, e.g. (27.9.2/2) "Calls to the function tmpnam with an argument of NULL may introduce a data race (17.6.5.9) with other calls to tmpnam with an argument of NULL."

OTHER TIPS

C++ standard doesn't guarantee new (or malloc()) to be thread-safe. Even though it's very crucial to have them thread-safe.

However, most platforms support thread-safe new.

Actually, C++ differentiates between "read-only thread safety" and "full thread safety". E.g. all std containers are "read-only" thread-safe and not safe if any of the threads modifies the container.

However, when you do not access shared data, I don't recall any of C++ features being unsafe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top