-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQuickThreads.cpp
More file actions
93 lines (66 loc) · 1.92 KB
/
Copy pathQuickThreads.cpp
File metadata and controls
93 lines (66 loc) · 1.92 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//--------------------------------------------------------------------
// QuickThreads.cpp.
// 11/11/2025. created.
// 11/13/2025. last modified.
//--------------------------------------------------------------------
// * QuickThreads - A Part of Agave(TM) Coroutine Framework
// (based on ISO C++20 or later).
// * if has any questions,
// * please contact me at 'full1900@outlook.com'.
// * by bubo.
//--------------------------------------------------------------------
#include "QuickThreads.h"
//--------------------------------------------------------------------
agave::QuickThreads::QuickThreads() :
_task_queue{ std::shared_ptr<Queue<ITask>>(new Queue<ITask>) }
{
//
}
//--------------------------------------------------------------------
agave::QuickThreads::~QuickThreads()
{
set_and_release_threads(true);
if (_task_queue)
while (auto task = _task_queue->pop())
delete task;
}
//--------------------------------------------------------------------
bool
agave::QuickThreads::add_task(ITask* task)
{
if (!task || !_task_queue)
return false;
_task_queue->push(task);
_cv.notify_one();
return true;
}
//--------------------------------------------------------------------
std::shared_ptr<agave::ITask>
agave::QuickThreads::join_and_get(void)
{
while (true)
{
if (auto task = _task_queue->pop())
return std::shared_ptr<ITask>{ task };
std::unique_lock lck{ _mx };
if (_is_exit)
return nullptr;
_cv.wait(lck);
if (_is_exit)
return nullptr;
}
}
//--------------------------------------------------------------------
void
agave::QuickThreads::set_and_release_threads(bool is_release /*= false*/)
{
if (is_release)
{
_is_exit = true;
std::unique_lock lck{ _mx };
_cv.notify_all();
}
else
_is_exit = false;
}
//--------------------------------------------------------------------