diff --git a/test/job/flag.cpp b/test/job/flag.cpp index 252ed3f0..0f4f7ced 100644 --- a/test/job/flag.cpp +++ b/test/job/flag.cpp @@ -34,5 +34,40 @@ main () tap.expect_eq (value, 2, "second wait didn't appear to block"); t2.join (); + { + // perform a stress test to (hopefully) discover deadlocks + // + // * create a large matrix of flag variables + // * create a bank of threads which: + // * wait on each flag of each row, or + // * notify if the flag index matches the thread index + constexpr int iterations = 1024; + constexpr int parallelism = 16; + + std::vector< + std::array + > flags (iterations); + + const auto func = [&flags] (const int idx) { + for (auto &row: flags) { + for (int i = 0; i < parallelism; ++i) { + if (i == idx) + row[i].notify (); + else + row[i].wait (); + } + } + }; + + std::vector workers; + for (int i = 0; i < parallelism; ++i) + workers.emplace_back (func, i); + + for (auto &t: workers) + t.join (); + + tap.expect (true, "flag sequence did not block"); + } + return tap.status (); -} \ No newline at end of file +}