diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2021-10-22 15:50:47 +0200 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2022-07-06 18:49:49 +0200 |
commit | 1af5a281460901281104532c9bb398adcf269eb9 (patch) | |
tree | 39aaeeb56a862b5de16776e39ff52e4d286e8c27 /test/buildtool/multithreading/task_system.test.cpp | |
parent | a71a4b5015327b15fb0bb4fea16c43f21ef0616c (diff) | |
download | justbuild-1af5a281460901281104532c9bb398adcf269eb9.tar.gz |
TaskSystem: Support wait for finish
Diffstat (limited to 'test/buildtool/multithreading/task_system.test.cpp')
-rw-r--r-- | test/buildtool/multithreading/task_system.test.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/buildtool/multithreading/task_system.test.cpp b/test/buildtool/multithreading/task_system.test.cpp index d34e3e4b..abcae8f4 100644 --- a/test/buildtool/multithreading/task_system.test.cpp +++ b/test/buildtool/multithreading/task_system.test.cpp @@ -222,3 +222,39 @@ TEST_CASE("All threads run until work is done", "[task_system]") { CHECK(tids.size() == kNumThreads); } } + +TEST_CASE("Use finish as system-wide barrier", "[task_system]") { + using namespace std::chrono_literals; + static auto const kNumThreads = std::thread::hardware_concurrency(); + + std::vector<int> vec(kNumThreads, 0); + std::vector<int> exp0(kNumThreads, 0); + std::vector<int> exp1(kNumThreads, 1); + std::vector<int> exp2(kNumThreads, 2); + + { + TaskSystem ts{kNumThreads}; + + // Wait for all threads to go to sleep. + ts.Finish(); + CHECK(vec == exp0); + + for (std::size_t i{}; i < ts.NumberOfThreads(); ++i) { + ts.QueueTask([&vec, i] { + std::this_thread::sleep_for(1s); + vec[i] = 1; + }); + } + + ts.Finish(); + CHECK(vec == exp1); + + for (std::size_t i{}; i < ts.NumberOfThreads(); ++i) { + ts.QueueTask([&vec, i] { + std::this_thread::sleep_for(1s); + vec[i] = 2; + }); + } + } + CHECK(vec == exp2); +} |