Created
September 2, 2020 04:03
-
-
Save sandeepkumar-skb/a2e765440bbd8fd35b5d6ae8106665d8 to your computer and use it in GitHub Desktop.
Sample atomic usage 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> | |
#include <atomic> | |
std::atomic<unsigned 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.load()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile & Run:
g++ -Wall -std=c++11 atomic_sample.cpp; ./a.out