"are constructors thread safe in c++ and/or c++11?" Code Answer


reasoning about thread-safety can be difficult, and i am no expert on the c++11 memory model. fortunately, however, your example is very simple. i rewrite the example, because the constructor is irrelevant.

simplified example

question: is the following code correct? or can the execution result in undefined behavior?

// legal transfer of pointer to int without data race.
// the receive function blocks until send is called.
void send(int*);
int* receive();

// --- thread a ---
/* a1 */   int* pointer = receive();
/* a2 */   int answer = *pointer;

// --- thread b ---
           int answer;
/* b1 */   answer = 42;
/* b2 */   send(&answer);
           // wait forever

answer: there may be a data race on the memory location of answer, and thus the execution results in undefined behavior. see below for details.

implementation of data transfer

of course, the answer depends on the possible and legal implementations of the functions send and receive. i use the following data-race-free implementation. note that only a single atomic variable is used, and all memory operations use std::memory_order_relaxed. basically this means, that these functions do not restrict memory re-orderings.

std::atomic<int*> transfer{nullptr};

void send(int* pointer) {
    transfer.store(pointer, std::memory_order_relaxed);

int* receive() {
    while (transfer.load(std::memory_order_relaxed) == nullptr) { }
    return transfer.load(std::memory_order_relaxed);

order of memory operations

on multicore systems, a thread can see memory changes in a different order as what other threads see. in addition, both compilers and cpus may reorder memory operations within a single thread for efficiency - and they do this all the time. atomic operations with std::memory_order_relaxed do not participate in any synchronization and do not impose any ordering.

in the above example, the compiler is allowed to reorder the operations of thread b, and execute b2 before b1, because the reordering has no effect on the thread itself.

// --- valid execution of operations in thread b ---
           int answer;
/* b2 */   send(&answer);
/* b1 */   answer = 42;
           // wait forever

data race

c++11 defines a data race as follows (n3290 c++11 draft): "the execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. any such data race results in undefined behavior." and the term happens before is defined earlier in the same document.

in the above example, b1 and a2 are conflicting and non-atomic operations, and neither happens before the other. this is obvious, because i have shown in the previous section, that both can happen at the same time.

that's the only thing that matters in c++11. in contrast, the java memory model also tries to define the behavior if there are data races, and it took them almost a decade to come up with a reasonable specification. c++11 didn't make the same mistake.

further information

i'm a bit surprised that these basics are not well known. the definitive source of information is the section multi-threaded executions and data races in the c++11 standard. however, the specification is difficult to understand.

a good starting point are hans boehm's talks - e.g. available as online videos:

  • threads and shared variables in c++11
  • getting c++ threads right

there are also a lot of other good resources, i have mentioned elsewhere, e.g.:

  • std::memory_order - cppreference.com
By Ghost Rider on August 1 2022

Answers related to “are constructors thread safe in c++ and/or c++11?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.