Skip to content

Instantly share code, notes, and snippets.

@choller
Created February 26, 2021 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choller/d625ecb4f245a4f466758b00dc254291 to your computer and use it in GitHub Desktop.
Save choller/d625ecb4f245a4f466758b00dc254291 to your computer and use it in GitHub Desktop.
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