I have a couple of questions regarding relaxed atomics in x86 architecture:
- If I understand correctly, all read/writes in types of up to 8 bytes are atomic by default. Thus, will there be any data race in the following code? Meaning, is there any possibility of the reader seeing a "half write"?
#include <iostream>
#include <thread>
uint32_t shared_var = 0;
void reader() {
while (shared_var != 42) {
std::cout << "Reader thread: " << shared_var << std::endl;
}
}
void writer() {
shared_var = 42;
std::cout << "Writer thread: " << shared_var << std::endl;
}
int main() {
std::thread t1(reader);
std::thread t2(writer);
t1.join();
t2.join();
return 0;
}
- If the answer to 1. is no, are there any difference with the following code:
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<uint32_t> shared_var = 0;
void reader() {
while (shared_var.load(std::memory_order_relaxed) != 42) {
std::cout << "Reader thread: " << shared_var << std::endl;
}
}
void writer() {
shared_var.store(42, std::memory_order_relaxed);
std::cout << "Writer thread: " << shared_var << std::endl;
}
int main() {
std::thread t1(reader);
std::thread t2(writer);
t1.join();
t2.join();
return 0;
}
- Can the 42 write in the following code be reorder before the reader? Assuming obviously that the write ocurred before the read.
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<uint32_t> shared_var = 0;
void reader() {
std::cout << "Reader thread: " << shared_var.load(std::memory_order_acquire) << std::endl;
}
void writer() {
shared_var.store(42, std::memory_order_relaxed);
std::cout << "Writer thread: " << shared_var << std::endl;
}
int main() {
std::thread t1(writer);
std::thread t2(reader);
t1.join();
t2.join();
return 0;
}