Add better tests (probabilistic and math?)
Create a test framework, where we can synthesize different random thread interleavings and if there is an error, then at least we should be able to display the interleaving which caused the error. (relacy?)
Would be nice to come up with mathematical tests as well. E.g. for copy_update there is a nice test to check that we'd not missed any update:
void test_sum_add() {
X x{};
int sum = 0;
std::thread t2{[&x, &sum]() {
executeInLoop<1000>([&x, &sum]() {
sum = x.sum();
});
}};
std::thread t1{[&x]() {
executeInLoop<1000>([&x]() {
x.add(3);
});
}};
std::thread t3{[&x]() {
executeInLoop<1000>([&x]() {
x.add(4);
});
}};
t1.join();
t2.join();
t3.join();
std::cout << x.sum() << std::endl;
ASSERT(x.sum() == 7000);
}
Another facility we might find handy to use is join_guard. We could either use the one in atomic_shared_ptr or implement another one. The key idea here would be to create a container of std::threads and pass them to a RAII object that would call join() on each of them if the givent thread is joinable().
auto threads = std::vector<std::thread>(3);
threads[1] = //...;
threads[0] = //...;
threads[2] = //..;
auto const _ = crf::make_join_guard(threads);
Please note: join_guard does not support move operations as of now but would be a good idea to extend it (in order to have a factory for it and reduce verbosity). which will happen soon enough. It would still remain NonCopyable.
What do you think?
Good idea, thx