ThreadPool: do not use joinable() internally

joinable() should be used only outside, since internally it is enough to
know `state` to know that something is wrong.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2022-07-13 10:01:31 +03:00
parent 3473b80077
commit 25eb82f120

View File

@ -198,7 +198,7 @@ public:
ThreadFromGlobalPool & operator=(ThreadFromGlobalPool && rhs) noexcept
{
if (joinable())
if (initialized())
abort();
state = std::move(rhs.state);
thread_id = std::move(rhs.thread_id);
@ -207,13 +207,13 @@ public:
~ThreadFromGlobalPool()
{
if (joinable())
if (initialized())
abort();
}
void join()
{
if (!joinable())
if (!initialized())
abort();
state->wait();
@ -222,7 +222,7 @@ public:
void detach()
{
if (!joinable())
if (!initialized())
abort();
state.reset();
}
@ -241,6 +241,14 @@ private:
/// The state used in this object and inside the thread job.
std::shared_ptr<Poco::Event> state;
std::shared_ptr<std::thread::id> thread_id;
/// Internally initialized() should be used over joinable(),
/// since it is enough to know that the thread is initialized,
/// and ignore that fact that thread cannot join itself.
bool initialized() const
{
return static_cast<bool>(state);
}
};