Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ktransformers
Commits
b4904537
Unverified
Commit
b4904537
authored
Oct 09, 2024
by
Chen Hongtao
Committed by
GitHub
Oct 09, 2024
Browse files
Merge pull request #83 from sayap/task-queue-cond-var
Use cond var to avoid busy loop
parents
43e8848d
6666d622
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
19 deletions
+26
-19
ktransformers/ktransformers_ext/cpu_backend/task_queue.cpp
ktransformers/ktransformers_ext/cpu_backend/task_queue.cpp
+24
-18
ktransformers/ktransformers_ext/cpu_backend/task_queue.h
ktransformers/ktransformers_ext/cpu_backend/task_queue.h
+2
-1
No files found.
ktransformers/ktransformers_ext/cpu_backend/task_queue.cpp
View file @
b4904537
...
...
@@ -16,17 +16,23 @@ TaskQueue::TaskQueue() {
}
TaskQueue
::~
TaskQueue
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
exit_flag
.
store
(
true
,
std
::
memory_order_seq_cst
);
}
cv
.
notify_all
();
if
(
worker
.
joinable
())
{
worker
.
join
();
}
}
void
TaskQueue
::
enqueue
(
std
::
function
<
void
()
>
task
)
{
mutex
.
lock
();
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
tasks
.
push
(
task
);
sync_flag
.
store
(
false
,
std
::
memory_order_seq_cst
);
mutex
.
unlock
();
}
cv
.
notify_one
();
}
void
TaskQueue
::
sync
()
{
...
...
@@ -36,22 +42,22 @@ void TaskQueue::sync() {
void
TaskQueue
::
processTasks
()
{
while
(
true
)
{
mutex
.
lock
();
if
(
tasks
.
empty
())
{
if
(
exit_flag
.
load
(
std
::
memory_order_seq_cst
))
{
std
::
function
<
void
()
>
task
;
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
cv
.
wait
(
lock
,
[
this
]()
{
return
!
tasks
.
empty
()
||
exit_flag
.
load
(
std
::
memory_order_seq_cst
);
});
if
(
exit_flag
.
load
(
std
::
memory_order_seq_cst
)
&&
tasks
.
empty
())
{
return
;
}
mutex
.
unlock
();
continue
;
task
=
tasks
.
front
();
tasks
.
pop
()
;
}
std
::
function
<
void
()
>
task
=
tasks
.
front
();
mutex
.
unlock
();
task
();
mutex
.
lock
();
tasks
.
pop
(
);
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex
);
if
(
tasks
.
empty
())
{
sync_flag
.
store
(
true
,
std
::
memory_order_seq_cst
);
}
mutex
.
unlock
();
}
}
}
ktransformers/ktransformers_ext/cpu_backend/task_queue.h
View file @
b4904537
...
...
@@ -69,8 +69,9 @@ class TaskQueue {
void
processTasks
();
std
::
queue
<
std
::
function
<
void
()
>>
tasks
;
std
::
mutex
mutex
;
std
::
condition_variable
cv
;
std
::
thread
worker
;
custom_mutex
mutex
;
std
::
atomic
<
bool
>
sync_flag
;
std
::
atomic
<
bool
>
exit_flag
;
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment