Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic C++ program with a simple data race
#include <iostream>
#include <thread>
int x = 0;
void print_x() {
std::cerr << "x is " << x << std::endl;
}
void set_x(int new_x) {
x = new_x;
}
void thread1() {
print_x();
}
void thread2() {
set_x(1);
}
int main() {
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment