Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Created September 2, 2020 04:03
Show Gist options
  • Save sandeepkumar-skb/a2e765440bbd8fd35b5d6ae8106665d8 to your computer and use it in GitHub Desktop.
Save sandeepkumar-skb/a2e765440bbd8fd35b5d6ae8106665d8 to your computer and use it in GitHub Desktop.
Sample atomic usage in C++
#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());
}
@sandeepkumar-skb
Copy link
Author

Compile & Run: g++ -Wall -std=c++11 atomic_sample.cpp; ./a.out

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