[K/N] Add ScopedThread

* thread that by default joins in the destructor (like C++20 jthread)
* can be given a name

Merge-request: KT-MR-5619
Merged-by: Alexander Shabalin <Alexander.Shabalin@jetbrains.com>
This commit is contained in:
Alexander Shabalin
2022-02-03 15:38:07 +00:00
committed by Space
parent 9826172720
commit 434213f416
23 changed files with 345 additions and 133 deletions
@@ -55,7 +55,7 @@ testing::MockFunction<void(PinnedContext&)>* PinnedContext::dtorMock = nullptr;
TEST(SingleThreadExecutorTest, ContextThreadBound) {
PinnedContext::ScopedMocks mocks;
PinnedContext* createdContext = nullptr;
std::thread::id createdThread;
ScopedThread::id createdThread;
EXPECT_CALL(mocks.ctorMock, Call(_)).WillOnce([&](PinnedContext& context) {
createdContext = &context;
createdThread = std::this_thread::get_id();
@@ -172,7 +172,7 @@ TEST(SingleThreadExecutorTest, ExecuteFromManyThreads) {
std::atomic_bool canStart = false;
KStdVector<int> expected;
KStdVector<std::thread> threads;
KStdVector<ScopedThread> threads;
for (int i = 0; i < kDefaultThreadCount; ++i) {
expected.push_back(i);
threads.emplace_back([&, i] {
@@ -184,9 +184,7 @@ TEST(SingleThreadExecutorTest, ExecuteFromManyThreads) {
canStart = true;
for (auto& thread : threads) {
thread.join();
}
threads.clear();
EXPECT_THAT(executor.context().result, testing::UnorderedElementsAreArray(expected));
}