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
8c901050
Commit
8c901050
authored
4 years ago
by
Joakim Skogø Langvand
Browse files
Options
Downloads
Patches
Plain Diff
Alotta cleanup
parent
21a87b52
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
+29
-16
29 additions, 16 deletions
workers.cpp
workers.hpp
+1
-1
1 addition, 1 deletion
workers.hpp
with
30 additions
and
17 deletions
workers.cpp
+
29
−
16
View file @
8c901050
...
...
@@ -17,34 +17,38 @@
#include
<chrono>
#include
<condition_variable>
#include
<cstdlib>
#include
<functional>
#include
<iostream>
#include
<mutex>
#include
<thread>
#include
<utility>
#include
"workers.hpp"
namespace
jlworkers
{
/* Construct an event loop
*/
Workers
::
Workers
()
{
this
->
m_running
=
false
;
this
->
m_runningThreadCount
=
0
;
this
->
m_runningTimeoutThreadCount
=
0
;
this
->
m_maxThreadCount
=
1
;
m_running
=
false
;
m_runningThreadCount
=
0
;
m_runningTimeoutThreadCount
=
0
;
m_maxThreadCount
=
1
;
}
/* Construct a concurent worker thread
*/
Workers
::
Workers
(
int
numThreads
)
{
this
->
m_running
=
false
;
this
->
m_runningThreadCount
=
0
;
this
->
m_runningTimeoutThreadCount
=
0
;
m_running
=
false
;
m_runningThreadCount
=
0
;
m_runningTimeoutThreadCount
=
0
;
// Ensure the number of threads is sane
this
->
m_maxThreadCount
=
numThreads
>
0
?
numThreads
:
1
;
m_maxThreadCount
=
numThreads
>
0
?
numThreads
:
1
;
}
/* Add a task to the queue
*/
void
Workers
::
post
(
const
std
::
function
<
void
()
>&
f
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
);
this
->
m_queue
.
emplace_back
(
f
);
m_queue
.
emplace_back
(
f
);
m_runningCondition
.
notify_all
();
}
...
...
@@ -61,6 +65,8 @@ namespace jlworkers {
}));
}
/* Start the main thread
*/
void
Workers
::
start
()
{
std
::
cout
<<
"Starting worker thread, "
<<
m_maxThreadCount
<<
" thread(s)
\n
"
;
...
...
@@ -93,19 +99,26 @@ namespace jlworkers {
});
}
/* Because the spec states there should be a stop() method
*/
void
Workers
::
stop
()
{
if
(
m_running
)
join
();
}
void
Workers
::
join
()
{
/* 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
()
||
m_runningTimeoutThreadCount
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
m_runningMutex
);
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
);
m_runningCondition
.
wait
(
lock
);
}
// Scope this to avoid deadlock
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
m_runningMutex
);
this
->
m_running
=
false
;
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
);
m_running
=
false
;
m_runningCondition
.
notify_all
();
}
...
...
This diff is collapsed.
Click to expand it.
workers.hpp
+
1
−
1
View file @
8c901050
...
...
@@ -54,7 +54,7 @@ namespace jlworkers {
public:
Workers
();
Workers
(
int
numThreads
);
~
Workers
()
{
if
(
m_running
)
join
();
}
~
Workers
()
{
stop
();
}
void
post
(
const
std
::
function
<
void
()
>&
);
void
post_timeout
(
const
std
::
function
<
void
()
>&
,
int
timeout
);
...
...
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