Last active
January 28, 2016 20:17
-
-
Save aprell/33ad3e3370568c8cf810 to your computer and use it in GitHub Desktop.
Atomic read-modify-write test
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 <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