Created
September 2, 2020 03:59
-
-
Save sandeepkumar-skb/e079d7163c142dee6bd906365ada8820 to your computer and use it in GitHub Desktop.
Creating a simplistic case of data race in C++
This file contains hidden or 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 <thread> | |
#include <stdio.h> | |
int count = 0; | |
void counter(){ | |
for (int i=0; i < 100000; ++i){ | |
count++; | |
} | |
} | |
int main(){ | |
std::thread counter1(counter); | |
std::thread counter2(counter); | |
counter1.join(); | |
counter2.join(); | |
printf("Final count is %d\n", count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile:
g++ -Wall -std=c++11 data_race.cpp -o data_race.o
Run:
./data_race.o