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
b3f17dea
Commit
b3f17dea
authored
4 years ago
by
Joakim Skogø Langvand
Browse files
Options
Downloads
Patches
Plain Diff
Remove debug output, tweak tests
parent
d4bcab61
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
build/eventloop
+0
-0
0 additions, 0 deletions
build/eventloop
main.cpp
+16
-11
16 additions, 11 deletions
main.cpp
workers.cpp
+4
-10
4 additions, 10 deletions
workers.cpp
with
20 additions
and
21 deletions
build/eventloop
+
0
−
0
View file @
b3f17dea
No preview for this file type
This diff is collapsed.
Click to expand it.
main.cpp
+
16
−
11
View file @
b3f17dea
...
...
@@ -9,24 +9,26 @@
#include
"workers.hpp"
auto
helloWorld
=
[](
int
i
)
{
std
::
cout
<<
"Task
"
<<
i
<<
"
\n
"
;
};
void
helloWorld
(
)
{
std
::
cout
<<
"Task
defined as regular (static) function
\n
"
;
}
void
workersTest
()
{
jlworkers
::
Workers
workerThread
(
16
);
void
workersTest
(
int
threads
)
{
jlworkers
::
Workers
workerThread
(
threads
);
std
::
mutex
printMutex
;
workerThread
.
post
([]()
{
/*
workerThread.post([]() {
std::cout << "Hello, World!\n";
});
});*/
workerThread
.
post
(
helloWorld
);
workerThread
.
start
();
workerThread
.
post
([]()
{
// Let's delay this a bit
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
500
));
std
::
cout
<<
"
Hello again, World!
\n
"
;
std
::
cout
<<
"
Anonymous function with 500ms sleep before print
\n
"
;
});
for
(
int
i
=
0
;
i
<
10
;
i
++
)
...
...
@@ -35,8 +37,6 @@ void workersTest() {
<<
", thread id "
<<
std
::
this_thread
::
get_id
()
<<
"
\n
"
;
});
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
1000
));
workerThread
.
stop
();
}
...
...
@@ -66,7 +66,12 @@ void stuff() {
int
main
(
int
argc
,
const
char
**
argv
)
{
workersTest
();
// Test workerthread
workersTest
(
16
);
// Test eventloop
workersTest
(
1
);
return
0
;
}
This diff is collapsed.
Click to expand it.
workers.cpp
+
4
−
10
View file @
b3f17dea
...
...
@@ -31,11 +31,12 @@ namespace jlworkers {
}
void
Workers
::
start
()
{
std
::
cout
<<
"Starting worker thread, "
<<
m_maxThreadCount
<<
" thread(s)
\n
"
;
m_running
=
true
;
std
::
cout
<<
"Starting task runner thread
\n
"
;
m_runnerThread
=
std
::
thread
([
this
]
{
while
(
m_running
)
{
std
::
cout
<<
"Queue: "
<<
m_queue
.
size
()
<<
"
\n
"
;
//while (!m_queue.empty()) {
while
(
m_runningThreadCount
<
m_maxThreadCount
&&
!
m_queue
.
empty
())
{
// Fetch next task in queue
...
...
@@ -54,21 +55,17 @@ namespace jlworkers {
//}
// Wait for any status updates
std
::
cout
<<
"Create lock and wait for update
\n
"
;
std
::
unique_lock
<
std
::
mutex
>
lock
(
m_runningMutex
);
m_runningCondition
.
wait
(
lock
);
}
std
::
cout
<<
"Queue empty
\n
"
;
for
(;
!
m_workers
.
empty
();
m_workers
.
pop_back
())
m_workers
.
back
().
join
();
});
}
void
Workers
::
stop
()
{
/* Sohuld we wait for queue to clear, or do we want to stop adding tasks?
* Let's do the
latt
er for now..
* Let's do the
form
er for now..
*/
std
::
cout
<<
"Stop runner thread; locking mutex
\n
"
;
while
(
!
m_queue
.
empty
())
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
m_runningMutex
);
m_runningCondition
.
wait
(
lock
);
...
...
@@ -78,13 +75,10 @@ namespace jlworkers {
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
m_runningMutex
);
this
->
m_running
=
false
;
std
::
cout
<<
"Notify threads
\n
"
;
m_runningCondition
.
notify_all
();
}
std
::
cout
<<
"Wait for runner to join
\n
"
;
m_runnerThread
.
join
();
std
::
cout
<<
"Runner done!
\n
"
;
}
}
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