diff options
| author | ptitSeb <sebastien.chev@gmail.com> | 2021-02-28 13:19:23 +0100 |
|---|---|---|
| committer | ptitSeb <sebastien.chev@gmail.com> | 2021-02-28 13:19:23 +0100 |
| commit | abee0fb56095633889ffe8dce9e04502d5566518 (patch) | |
| tree | d7512eecbfe974e7f23298a1ef634cae35abb9eb /tests/test10.cpp | |
| parent | 80a8bf0ce1d73d1f8b3f8d1aebd60106a1c39351 (diff) | |
| download | box64-abee0fb56095633889ffe8dce9e04502d5566518.tar.gz box64-abee0fb56095633889ffe8dce9e04502d5566518.zip | |
Added x86_64 tests (based on box86)
Diffstat (limited to 'tests/test10.cpp')
| -rw-r--r-- | tests/test10.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test10.cpp b/tests/test10.cpp new file mode 100644 index 00000000..f3c61316 --- /dev/null +++ b/tests/test10.cpp @@ -0,0 +1,27 @@ +// using atomic as a lock +#include <iostream> // std::cout +#include <atomic> // std::atomic +#include <thread> // std::thread +#include <vector> // std::vector +#include <sstream> // std::stringstream + +std::atomic<bool> lock_stream[10]; +std::stringstream stream; + +void append_number(int x) { + while (lock_stream[x].load()) {} + stream << "thread #" << x << '\n'; + if (x != 9) lock_stream[x + 1].store(false); +} + +int main () +{ + std::vector<std::thread> threads; + for (int i = 0; i < 10; ++i) lock_stream[i].store(true); + for (int i=0; i<10; ++i) threads.push_back(std::thread(append_number,i)); + lock_stream[0].store(false); + for (auto& th : threads) th.join(); + + std::cout << stream.str(); + return 0; +} |