Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Network Programming assignment 4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container 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
Joakim Skogø Langvand
Network Programming assignment 4
Commits
229fdffb
Commit
229fdffb
authored
4 years ago
by
Joakim Skogø Langvand
Browse files
Options
Downloads
Patches
Plain Diff
Better post_timeout()
parent
41481086
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
workers.cpp
+10
-6
10 additions, 6 deletions
workers.cpp
workers.hpp
+1
-0
1 addition, 0 deletions
workers.hpp
with
11 additions
and
6 deletions
workers.cpp
+
10
−
6
View file @
229fdffb
...
...
@@ -13,12 +13,14 @@ namespace jlworkers {
Workers
::
Workers
()
{
this
->
m_running
=
false
;
this
->
m_runningThreadCount
=
0
;
this
->
m_runningTimeoutThreadCount
=
0
;
this
->
m_maxThreadCount
=
1
;
}
Workers
::
Workers
(
int
numThreads
)
{
this
->
m_running
=
false
;
this
->
m_runningThreadCount
=
0
;
this
->
m_runningTimeoutThreadCount
=
0
;
// Ensure the number of threads is sane
this
->
m_maxThreadCount
=
numThreads
>
0
?
numThreads
:
1
;
}
...
...
@@ -32,12 +34,14 @@ namespace jlworkers {
/* Crude implementation of timeout method.
*/
void
Workers
::
post_timeout
(
const
std
::
function
<
void
()
>&
f
,
int
timeout
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
)
;
this
->
m_queue
.
emplace_back
(
[
f
,
timeout
]
{
m_runningTimeoutThreadCount
++
;
m_workers
.
emplace_back
(
std
::
thread
([
this
,
f
,
timeout
]
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
timeout
));
f
();
});
post
(
f
);
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
);
m_runningTimeoutThreadCount
--
;
m_runningCondition
.
notify_all
();
}));
}
void
Workers
::
start
()
{
...
...
@@ -76,7 +80,7 @@ namespace jlworkers {
/* Sohuld we wait for queue to clear, or do we want to stop adding tasks?
* Let's do the former for now..
*/
while
(
!
m_queue
.
empty
())
{
while
(
!
m_queue
.
empty
()
||
m_runningTimeoutThreadCount
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
m_runningMutex
);
m_runningCondition
.
wait
(
lock
);
}
...
...
This diff is collapsed.
Click to expand it.
workers.hpp
+
1
−
0
View file @
229fdffb
...
...
@@ -21,6 +21,7 @@ namespace jlworkers {
private:
int
m_maxThreadCount
;
std
::
atomic_int
m_runningThreadCount
;
std
::
atomic_int
m_runningTimeoutThreadCount
;
std
::
atomic_bool
m_running
;
std
::
condition_variable
m_runningCondition
;
...
...
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