Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
instant-ngp-tomography
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Pickles
instant-ngp-tomography
Commits
a2ba0e63
Commit
a2ba0e63
authored
2 years ago
by
Thomas Müller
Browse files
Options
Downloads
Patches
Plain Diff
`ThreadPool` simplifications
parent
df85fe95
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/neural-graphics-primitives/thread_pool.h
+1
-12
1 addition, 12 deletions
include/neural-graphics-primitives/thread_pool.h
src/thread_pool.cpp
+5
-30
5 additions, 30 deletions
src/thread_pool.cpp
with
6 additions
and
42 deletions
include/neural-graphics-primitives/thread_pool.h
+
1
−
12
View file @
a2ba0e63
...
@@ -41,8 +41,6 @@ public:
...
@@ -41,8 +41,6 @@ public:
auto
enqueueTask
(
F
&&
f
,
bool
highPriority
=
false
)
->
std
::
future
<
std
::
result_of_t
<
F
()
>>
{
auto
enqueueTask
(
F
&&
f
,
bool
highPriority
=
false
)
->
std
::
future
<
std
::
result_of_t
<
F
()
>>
{
using
return_type
=
std
::
result_of_t
<
F
()
>
;
using
return_type
=
std
::
result_of_t
<
F
()
>
;
++
mNumTasksInSystem
;
auto
task
=
std
::
make_shared
<
std
::
packaged_task
<
return_type
()
>>
(
std
::
forward
<
F
>
(
f
));
auto
task
=
std
::
make_shared
<
std
::
packaged_task
<
return_type
()
>>
(
std
::
forward
<
F
>
(
f
));
auto
res
=
task
->
get_future
();
auto
res
=
task
->
get_future
();
...
@@ -63,13 +61,8 @@ public:
...
@@ -63,13 +61,8 @@ public:
void
startThreads
(
size_t
num
);
void
startThreads
(
size_t
num
);
void
shutdownThreads
(
size_t
num
);
void
shutdownThreads
(
size_t
num
);
void
setNThreads
(
size_t
num
);
size_t
numTasksInSystem
()
const
{
return
mNumTasksInSystem
;
}
void
waitUntilFinished
();
void
waitUntilFinishedFor
(
const
std
::
chrono
::
microseconds
Duration
);
void
flushQueue
();
void
flushQueue
();
template
<
typename
Int
,
typename
F
>
template
<
typename
Int
,
typename
F
>
...
@@ -109,10 +102,6 @@ private:
...
@@ -109,10 +102,6 @@ private:
std
::
deque
<
std
::
function
<
void
()
>>
mTaskQueue
;
std
::
deque
<
std
::
function
<
void
()
>>
mTaskQueue
;
std
::
mutex
mTaskQueueMutex
;
std
::
mutex
mTaskQueueMutex
;
std
::
condition_variable
mWorkerCondition
;
std
::
condition_variable
mWorkerCondition
;
std
::
atomic
<
size_t
>
mNumTasksInSystem
;
std
::
mutex
mSystemBusyMutex
;
std
::
condition_variable
mSystemBusyCondition
;
};
};
NGP_NAMESPACE_END
NGP_NAMESPACE_END
This diff is collapsed.
Click to expand it.
src/thread_pool.cpp
+
5
−
30
View file @
a2ba0e63
...
@@ -28,7 +28,6 @@ ThreadPool::ThreadPool(size_t maxNumThreads, bool force) {
...
@@ -28,7 +28,6 @@ ThreadPool::ThreadPool(size_t maxNumThreads, bool force) {
maxNumThreads
=
min
((
size_t
)
thread
::
hardware_concurrency
(),
maxNumThreads
);
maxNumThreads
=
min
((
size_t
)
thread
::
hardware_concurrency
(),
maxNumThreads
);
}
}
startThreads
(
maxNumThreads
);
startThreads
(
maxNumThreads
);
mNumTasksInSystem
.
store
(
0
);
}
}
ThreadPool
::~
ThreadPool
()
{
ThreadPool
::~
ThreadPool
()
{
...
@@ -59,16 +58,6 @@ void ThreadPool::startThreads(size_t num) {
...
@@ -59,16 +58,6 @@ void ThreadPool::startThreads(size_t num) {
lock
.
unlock
();
lock
.
unlock
();
task
();
task
();
mNumTasksInSystem
--
;
{
unique_lock
<
mutex
>
localLock
{
mSystemBusyMutex
};
if
(
mNumTasksInSystem
==
0
)
{
mSystemBusyCondition
.
notify_all
();
}
}
}
}
});
});
}
}
...
@@ -90,30 +79,16 @@ void ThreadPool::shutdownThreads(size_t num) {
...
@@ -90,30 +79,16 @@ void ThreadPool::shutdownThreads(size_t num) {
}
}
}
}
void
ThreadPool
::
waitUntilFinished
()
{
void
ThreadPool
::
setNThreads
(
size_t
num
)
{
unique_lock
<
mutex
>
lock
{
mSystemBusyMutex
};
if
(
mNumThreads
>
num
)
{
shutdownThreads
(
mNumThreads
-
num
);
if
(
mNumTasksInSystem
==
0
)
{
}
else
if
(
mNumThreads
<
num
)
{
return
;
startThreads
(
num
-
mNumThreads
);
}
mSystemBusyCondition
.
wait
(
lock
);
}
void
ThreadPool
::
waitUntilFinishedFor
(
const
chrono
::
microseconds
Duration
)
{
unique_lock
<
mutex
>
lock
{
mSystemBusyMutex
};
if
(
mNumTasksInSystem
==
0
)
{
return
;
}
}
mSystemBusyCondition
.
wait_for
(
lock
,
Duration
);
}
}
void
ThreadPool
::
flushQueue
()
{
void
ThreadPool
::
flushQueue
()
{
lock_guard
<
mutex
>
lock
{
mTaskQueueMutex
};
lock_guard
<
mutex
>
lock
{
mTaskQueueMutex
};
mNumTasksInSystem
-=
mTaskQueue
.
size
();
mTaskQueue
.
clear
();
mTaskQueue
.
clear
();
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment