Chapter 0 · Section 6 of 8 · 2 min read
Other languages
The lost update is not a Go problem. Here is the same unprotected counter in C++ and Python, run on this machine, and what each language's documentation says about it.
C++
The C++ version uses a plain int that eight threads increment 100,000 times each:
// An unprotected shared counter in C++. Compile and run:
// clang++ -std=c++20 -pthread counter.cpp -o counter && ./counter
// With the thread sanitizer (it reports the data race):
// clang++ -std=c++20 -pthread -fsanitize=thread counter.cpp -o counter && ./counter
#include <iostream>
#include <thread>
#include <vector>
int main() {
const int workers = 8, per = 100000;
int count = 0; // plain int shared by every thread
std::vector<std::thread> threads;
for (int i = 0; i < workers; i++) {
threads.emplace_back([&] {
for (int j = 0; j < per; j++) count++;
});
}
for (auto& t : threads) t.join();
std::cout << "expected " << workers * per << ", got " << count << "\n";
}Built with Apple clang 21 and run twice, it printed:
expected 800000, got 233165
expected 800000, got 242163Built again with -fsanitize=thread, the thread sanitizer reported WARNING: ThreadSanitizer: data race for the write to the counter. That is the C++ counterpart of Go's -race.
What the C++ documentation says: "If a data race occurs, the behavior of the program is undefined." (Multi-threaded executions and data races, cppreference.com.) The page also says that releasing a std::mutex is synchronized-with acquiring it in another thread, "which makes it possible to use mutex locks to guard against data races." Undefined behaviour is a stronger statement than "you might lose an update": the language puts no limit on what the program may do. The lost counts above are what happened to appear on one compiler at its default settings.
Python
The Python version has eight threads, each adding 1 a hundred thousand times:
# An unprotected shared counter in Python. Run: python3 counter.py
import sys
import threading
workers, per = 8, 100_000
count = 0
def work():
global count
for _ in range(per):
count += 1
threads = [threading.Thread(target=work) for _ in range(workers)]
for t in threads:
t.start()
for t in threads:
t.join()
gil = getattr(sys, "_is_gil_enabled", lambda: True)()
print(f"Python {sys.version.split()[0]}, GIL enabled: {gil}")
print(f"expected {workers * per}, got {count}")Run on Python 3.14.0 (GIL enabled) three times, it printed expected 800000, got 800000 every time. No update was lost in my runs.
That does not mean the code is safe. The Python FAQ lists i = i + 1 among operations that are not atomic, and says "When in doubt, use a mutex!" (Python FAQ). Thread switches happen between bytecode instructions, and a given run may simply never switch at the unlucky point. The same code on a different version, a free-threaded build or a busier machine can behave differently.
Rust
Safe Rust does not let you write this program. Two threads cannot both hold a mutable reference to the same integer, so the compiler rejects an unprotected shared counter. To share one, you wrap it in a type that makes the sharing safe, such as an atomic (the Rustonomicon's own example) or a Mutex. (The Rustonomicon.) I did not have a Rust compiler on this machine, so there is no Rust program to run here.
What the three answers tell you
| Language | Unprotected shared counter | Source |
|---|---|---|
| Go | A data race. A detector reports it with -race. | go.dev |
| C++ | A data race, and undefined behaviour. A sanitizer can report it. | cppreference |
| Python | Not atomic per the FAQ, though the GIL can hide it in a given run. | docs.python.org |
| Rust (safe) | Does not compile. | Rustonomicon |
Java appears from Chapter 1 on, where its lock classes have documentation worth quoting.