Skip to content

The Myth of Thread Safety

1. Introduction

Many developers believe that using const, atomic operations, or simple synchronization primitives automatically makes their code thread-safe. In reality, thread safety is more nuanced and requires careful design.

2. Common Misconceptions

2.1 "const Makes It Thread-Safe"

class Config {
    std::string name_;
public:
    const std::string& name() const { return name_; }
};

// Two threads reading the same Config object
Config config;
// Thread 1: config.name()  -- OK, const
// Thread 2: config.name()  -- OK, const
// But what if one thread is modifying config while others read?

Reality: const prevents accidental modification but doesn't provide thread safety. If another thread modifies the object, reading from a const reference is still a data race.

2.2 "Atomic Operations Are Always Safe"

std::atomic<int> counter{0};

// Thread 1
counter++;  // Atomic increment

// Thread 2
counter++;  // Atomic increment

// Problem: Is this the same as counter += 2? Not necessarily!
// The order of operations may be: T1 reads, T2 reads, T1 writes, T2 writes

Reality: Atomic operations are safe for individual operations, but compound operations (read-modify-write sequences) may still have race conditions.

2.3 "One Lock Per Object Is Enough"

class BankAccount {
    mutable std::mutex mtx_;
    int balance_;
public:
    void transfer(BankAccount& to, int amount) {
        std::lock_guard<std::mutex> lock(mtx_);
        balance_ -= amount;
        to.balance_ += amount;  // DEADLOCK: another thread may hold to.mtx_
    }
};

Reality: Locking individual objects doesn't prevent deadlocks when multiple objects are involved.

3. Subtle Thread Safety Issues

3.1 Non-Atomic Compound Operations

class Counter {
    int value_ = 0;
public:
    void increment() {
        value_++;  // Not atomic: read, increment, write
    }
};

// Two threads calling increment() simultaneously:
// Thread 1: reads 0, increments to 1, writes 1
// Thread 2: reads 0 (before thread 1 writes), increments to 1, writes 1
// Result: value_ is 1, should be 2

Fix: Use std::atomic<int> or proper synchronization.

3.2 Iterator Invalidation

std::vector<int> vec = {1, 2, 3, 4, 5};

// Thread 1
for (auto it = vec.begin(); it != vec.end(); ++it) {
    // Thread 2 modifies vec here
    std::cout << *it << '\n';  // Iterator may be invalid
}

Fix: Use proper synchronization or thread-safe containers.

3.3 Memory Order

std::atomic<bool> ready{false};
int data = 0;

// Thread 1
data = 42;
ready = true;  // Memory order: seq_cst (default)

// Thread 2
while (!ready) {}
std::cout << data << '\n';  // May still see 0!

Reality: Without proper memory ordering, the compiler and CPU may reorder operations.

3.4 Single-Threaded Libraries

// Some libraries are not thread-safe
class NonThreadSafeLib {
public:
    void process() {
        // Uses static internal state
        static int count = 0;
        count++;
    }
};

// Two threads calling process() simultaneously: data race!

4. Proper Synchronization Patterns

4.1 Mutex Locking

class ThreadSafeCounter {
    mutable std::mutex mtx_;
    int value_ = 0;
public:
    void increment() {
        std::lock_guard<std::mutex> lock(mtx_);
        value_++;
    }
    int get() const {
        std::lock_guard<std::mutex> lock(mtx_);
        return value_;
    }
};

4.2 Reader-Writer Lock

class ConcurrentMap {
    mutable std::shared_mutex mtx_;
    std::map<std::string, int> data_;
public:
    int get(const std::string& key) const {
        std::shared_lock<std::shared_mutex> lock(mtx_);  // Multiple readers allowed
        return data_.at(key);
    }
    void set(const std::string& key, int value) {
        std::unique_lock<std::shared_mutex> lock(mtx_);  // Exclusive access
        data_[key] = value;
    }
};

4.3 Lock-Free Programming

class LockFreeQueue {
    std::atomic<Node*> head_{nullptr};
public:
    void push(int value) {
        Node* node = new Node(value);
        node->next = head_.load();
        while (!head_.compare_exchange_weak(node->next, node)) {}
    }
};

5. Testing Thread Safety

5.1 Stress Testing

Run multiple threads simultaneously and check for: - Data races (use ThreadSanitizer) - Deadlocks - Correctness

5.2 Formal Verification

For critical code, consider formal verification tools or proofs.

6. Best Practices

  1. Document thread safety — Clearly state which operations are thread-safe
  2. Use proven patterns — Mutex, atomic, thread-local storage
  3. Minimize shared state — The best synchronization is no sharing
  4. Avoid premature optimization — Simple locks are often fast enough
  5. Test under load — Thread bugs often appear only under heavy concurrency
  6. Use tools — ThreadSanitizer, helgrind, drd

Example

A runnable version of this topic:

7. Summary

  • const doesn't guarantee thread safety
  • Atomic operations are safe individually but not for compound operations
  • Proper synchronization requires understanding the problem domain
  • Document thread safety requirements clearly
  • Test thoroughly under concurrent load