libcruft-util/test/job/flag.cpp

38 lines
783 B
C++

#include "job/flag.hpp"
#include "tap.hpp"
#include <thread>
int
main ()
{
util::TAP::logger tap;
util::job::flag f;
std::atomic<int> value = 0;
std::thread t1 ([&] () {
f.wait ();
value = 1;
});
std::this_thread::sleep_for (std::chrono::milliseconds (100));
tap.expect_eq (value, 0, "value hasn't been set during wait");
tap.expect_eq (f.notify (), 1, "notify reports one thread woke");
t1.join ();
tap.expect_eq (value, 1, "value has been changed after wait");
std::thread t2 ([&] () {
f.wait ();
value = 2;
});
std::this_thread::sleep_for (std::chrono::milliseconds (100));
tap.expect_eq (value, 2, "second wait didn't appear to block");
t2.join ();
return tap.status ();
}