Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F131912
thread_pool.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
thread_pool.cpp
View Options
// Copyright 2021 Practically.io All rights reserved
//
// Use of this source is governed by a BSD-style
// licence that can be found in the LICENCE file or at
// https://www.practically.io/copyright/
#include
"thread_pool.hpp"
namespace
ivy
{
void
ThreadPool::run_job
()
{
std
::
function
<
void
()
>
job
;
while
(
true
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_queue_lock
);
m_condition
.
wait
(
lock
,
[
&
]()
{
return
!
m_queue
.
empty
()
||
m_stop
;
});
if
(
m_queue
.
empty
())
{
return
;
}
job
=
m_queue
.
front
();
m_queue
.
pop
();
}
job
();
{
// Only decrement the job count when the job has finished running.
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_count_lock
);
m_job_count
--
;
}
}
}
void
ThreadPool::create_threads
(
unsigned
int
thread_count
)
{
for
(
int
i
=
0
;
i
<
thread_count
;
i
++
)
{
m_threads
.
emplace_back
(
std
::
thread
([
this
]
{
run_job
();
}));
}
}
void
ThreadPool::push
(
std
::
function
<
void
()
>
job
)
{
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_count_lock
);
m_job_count
++
;
}
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_queue_lock
);
m_queue
.
push
(
job
);
}
m_condition
.
notify_one
();
}
bool
ThreadPool::empty
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_count_lock
);
return
m_job_count
==
0
;
}
void
ThreadPool::shutdown
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_queue_lock
);
m_stop
=
true
;
}
m_condition
.
notify_all
();
for
(
auto
&
thread
:
m_threads
)
{
thread
.
join
();
}
}
}
// namespace ivy
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Apr 6 2026, 6:39 PM (5 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15638
Default Alt Text
thread_pool.cpp (1 KB)
Attached To
Mode
R1 ivy.nvim
Attached
Detach File
Event Timeline
Log In to Comment