Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Created September 2, 2020 04:01
Show Gist options
  • Save sandeepkumar-skb/797b27b212630d02fe46f5bba41913ec to your computer and use it in GitHub Desktop.
Save sandeepkumar-skb/797b27b212630d02fe46f5bba41913ec to your computer and use it in GitHub Desktop.
#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);
}
@sandeepkumar-skb
Copy link
Author

Compile and Run:
g++ -Wall -std=c++11 mutex_sample.cpp -o mutex.o; ./mutex.o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment