job/flag: improved parallelism test

This commit is contained in:
Danny Robson 2018-03-20 18:02:20 +11:00
parent e120540bb9
commit 82d63f68b1

View File

@ -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<util::job::flag,parallelism>
> 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<std::thread> 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 ();
}