Created
September 2, 2020 04:01
-
-
Save sandeepkumar-skb/797b27b212630d02fe46f5bba41913ec to your computer and use it in GitHub Desktop.
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> | |
#include <mutex> | |
int count = 0; | |
std::mutex writer; | |
void counter(){ | |
for (int i=0; i < 100000; ++i){ | |
writer.lock(); | |
count++; | |
writer.unlock(); | |
} | |
} | |
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 and Run:
g++ -Wall -std=c++11 mutex_sample.cpp -o mutex.o; ./mutex.o