-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathThreadTask.cpp
More file actions
67 lines (56 loc) · 1.31 KB
/
ThreadTask.cpp
File metadata and controls
67 lines (56 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/utils/threading/ThreadTask.h>
#include <aws/core/utils/threading/PooledThreadExecutor.h>
using namespace Aws::Utils;
using namespace Aws::Utils::Threading;
ThreadTask::ThreadTask(PooledThreadExecutor& executor)
: m_executor(executor),
m_continue(true),
m_detached(false),
m_thread([this]() { this->MainTaskRunner(); }) // lambda captures this
{
}
ThreadTask::~ThreadTask()
{
StopProcessingWork();
if (!m_detached) {
m_thread.join();
} else {
m_thread.detach();
}
}
void ThreadTask::MainTaskRunner()
{
while (m_continue)
{
while (m_continue && m_executor.HasTasks())
{
auto fn = m_executor.PopTask();
if(fn)
{
(*fn)();
Aws::Delete(fn);
}
}
if(m_continue)
{
m_executor.m_sync.WaitOne();
}
}
if (m_detached) {
Aws::Delete(this);
}
}
void ThreadTask::StopProcessingWork()
{
m_continue = false;
}
std::thread::id ThreadTask::GetThreadId() const {
return m_thread.get_id();
}
void ThreadTask::DetachFromExecutor() {
m_detached = true;
}