Skip to content

Commit 08b671e

Browse files
committed
fixed crash in thread pool and added test
1 parent 155b318 commit 08b671e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

cloud/storage/core/libs/common/thread_pool.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class TThreadPool final
8888
worker.Name = TStringBuilder() << threadName << i++;
8989
worker.Thread = std::make_unique<TWorkerThread>(*this, worker);
9090
}
91+
AtomicSet(RunningWorkers, NumWorkers);
9192
}
9293

9394
~TThreadPool() override
@@ -97,8 +98,6 @@ class TThreadPool final
9798

9899
void Start() override
99100
{
100-
AtomicSet(RunningWorkers, NumWorkers);
101-
102101
for (auto& worker: Workers) {
103102
worker.Thread->Start();
104103
}

cloud/storage/core/libs/common/thread_pool_ut.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <util/generic/scope.h>
88

9+
#include <thread>
10+
911
namespace NCloud {
1012

1113
////////////////////////////////////////////////////////////////////////////////
@@ -30,6 +32,37 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest)
3032

3133
UNIT_ASSERT_EQUAL(future.GetValue(WaitTimeout), 42);
3234
}
35+
36+
Y_UNIT_TEST(ShouldExecuteTaskEnqueuedBeforeStart)
37+
{
38+
auto threadPool = CreateThreadPool("thread", 1);
39+
40+
auto promise = NThreading::NewPromise();
41+
42+
auto future = promise.GetFuture();
43+
44+
std::thread thread(
45+
[threadPool, promise = std::move(promise)]() mutable
46+
{
47+
promise.SetValue();
48+
auto future = threadPool->Execute([] { return 42; });
49+
50+
UNIT_ASSERT_EQUAL(future.GetValue(WaitTimeout), 42);
51+
});
52+
53+
future.GetValueSync();
54+
55+
// Sleep to be sure that task will be enqueued before start.
56+
Sleep(TDuration::Seconds(1));
57+
58+
threadPool->Start();
59+
Y_DEFER
60+
{
61+
threadPool->Stop();
62+
};
63+
64+
thread.join();
65+
}
3366
}
3467

3568
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)