Created
February 26, 2021 12:59
-
-
Save choller/d625ecb4f245a4f466758b00dc254291 to your computer and use it in GitHub Desktop.
Basic C++ program with a simple data race
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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