}
/// Set the readiness of the main thread.
- void setMainThreadReadyState(bool Ready) {
- std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
- MainThreadReady = Ready;
+ void setMainThreadReady() {
+ {
+ std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
+ MainThreadReady = true;
+ }
WaitMainThread.notify_all();
}
+ void SetUp() override { MainThreadReady = false; }
+
std::condition_variable WaitMainThread;
std::mutex WaitMainThreadMutex;
bool MainThreadReady;
std::atomic_int checked_in{0};
- setMainThreadReadyState(false);
ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] {
});
}
ASSERT_EQ(0, checked_in);
- setMainThreadReadyState(true);
+ setMainThreadReady();
Pool.wait();
ASSERT_EQ(5, checked_in);
}
CHECK_UNSUPPORTED();
ThreadPool Pool;
std::atomic_int i{0};
- setMainThreadReadyState(false);
Pool.async([this, &i] {
waitForMainThread();
++i;
});
Pool.async([&i] { ++i; });
ASSERT_NE(2, i.load());
- setMainThreadReadyState(true);
+ setMainThreadReady();
Pool.wait();
ASSERT_EQ(2, i.load());
}
CHECK_UNSUPPORTED();
ThreadPool Pool;
std::atomic_int i{0};
- setMainThreadReadyState(false);
Pool.async([this, &i] {
waitForMainThread();
++i;
// Force the future using get()
Pool.async([&i] { ++i; }).get();
ASSERT_NE(2, i.load());
- setMainThreadReadyState(true);
+ setMainThreadReady();
Pool.wait();
ASSERT_EQ(2, i.load());
}
// Test that we are waiting on destruction
std::atomic_int checked_in{0};
{
- setMainThreadReadyState(false);
ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] {
});
}
ASSERT_EQ(0, checked_in);
- setMainThreadReadyState(true);
+ setMainThreadReady();
}
ASSERT_EQ(5, checked_in);
}