[K/N] Fix ScopedThreadTest
Merge-request: KT-MR-6744 Merged-by: Alexander Shabalin <Alexander.Shabalin@jetbrains.com>
This commit is contained in:
committed by
Space
parent
80450042c7
commit
29a0bbca4d
@@ -64,7 +64,8 @@ TEST(ScopedThreadTest, EmptyThreadName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(ScopedThreadTest, JoinsInDestructor) {
|
TEST(ScopedThreadTest, JoinsInDestructor) {
|
||||||
std::atomic<bool> exited = false;
|
// Not atomic for TSAN to complain if we didn't synchronize with join.
|
||||||
|
bool exited = false;
|
||||||
{
|
{
|
||||||
ScopedThread([&exited] {
|
ScopedThread([&exited] {
|
||||||
// Give a chance for the outer scope to go on.
|
// Give a chance for the outer scope to go on.
|
||||||
@@ -72,11 +73,12 @@ TEST(ScopedThreadTest, JoinsInDestructor) {
|
|||||||
exited = true;
|
exited = true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
EXPECT_THAT(exited.load(), true);
|
EXPECT_THAT(exited, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ScopedThreadTest, ManualJoin) {
|
TEST(ScopedThreadTest, ManualJoin) {
|
||||||
std::atomic<bool> exited = false;
|
// Not atomic for TSAN to complain if we didn't synchronize with join.
|
||||||
|
bool exited = false;
|
||||||
ScopedThread thread([&exited] {
|
ScopedThread thread([&exited] {
|
||||||
// Give a chance for the outer scope to go on.
|
// Give a chance for the outer scope to go on.
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
@@ -85,15 +87,15 @@ TEST(ScopedThreadTest, ManualJoin) {
|
|||||||
EXPECT_THAT(thread.joinable(), true);
|
EXPECT_THAT(thread.joinable(), true);
|
||||||
thread.join();
|
thread.join();
|
||||||
EXPECT_THAT(thread.joinable(), false);
|
EXPECT_THAT(thread.joinable(), false);
|
||||||
EXPECT_THAT(exited.load(), true);
|
EXPECT_THAT(exited, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ScopedThreadTest, Detach) {
|
TEST(ScopedThreadTest, Detach) {
|
||||||
|
std::atomic<bool> doExit = false;
|
||||||
std::atomic<bool> exited = false;
|
std::atomic<bool> exited = false;
|
||||||
{
|
{
|
||||||
ScopedThread thread([&exited] {
|
ScopedThread thread([&doExit, &exited] {
|
||||||
// Give a chance for the outer scope to go on.
|
while (!doExit.load()) {}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
||||||
exited = true;
|
exited = true;
|
||||||
});
|
});
|
||||||
EXPECT_THAT(thread.joinable(), true);
|
EXPECT_THAT(thread.joinable(), true);
|
||||||
@@ -101,8 +103,7 @@ TEST(ScopedThreadTest, Detach) {
|
|||||||
EXPECT_THAT(thread.joinable(), false);
|
EXPECT_THAT(thread.joinable(), false);
|
||||||
}
|
}
|
||||||
EXPECT_THAT(exited.load(), false);
|
EXPECT_THAT(exited.load(), false);
|
||||||
// Wait for the thread to set `exited` before terminating test, otherwise the thread will write
|
doExit = true;
|
||||||
// into freed `exited`.
|
|
||||||
while (!exited.load()) {
|
while (!exited.load()) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user