Skip to content

Instantly share code, notes, and snippets.

@aprell
Last active January 28, 2016 20:17
Show Gist options
  • Select an option

  • Save aprell/33ad3e3370568c8cf810 to your computer and use it in GitHub Desktop.

Select an option

Save aprell/33ad3e3370568c8cf810 to your computer and use it in GitHub Desktop.
Atomic read-modify-write test
#include <vector>
#include <thread>
#include <atomic>
#include <cassert>
int main()
{
std::vector<std::thread> threads(16);
std::atomic<unsigned int> ai(0);
for (size_t i = 0; i < threads.size(); ++i) {
threads[i] = std::thread([&ai, i] {
for (auto j = 0; j < 1000; ++j) {
ai += i;
}
});
}
for (auto &t : threads) {
t.join();
}
auto res = 500 * threads.size() * (threads.size() - 1);
assert(ai == res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment