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
81a5a218
Commit
81a5a218
authored
4 years ago
by
Joakim Skogø Langvand
Browse files
Options
Downloads
Patches
Plain Diff
Licence
parent
229fdffb
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
main.cpp
+59
-64
59 additions, 64 deletions
main.cpp
workers.cpp
+17
-0
17 additions, 0 deletions
workers.cpp
with
76 additions
and
64 deletions
main.cpp
+
59
−
64
View file @
81a5a218
/*
* Copyright (C) 2021 Joakim Skogø Langvand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
<atomic>
#include
<bits/types/struct_itimerspec.h>
#include
<chrono>
#include
<condition_variable>
#include
<ctime>
#include
<iostream>
#include
<mutex>
#include
<ostream>
#include
<ratio>
#include
<sys/epoll.h>
#include
<sys/timerfd.h>
#include
<thread>
#include
<vector>
#include
"workers.hpp"
void
helloWorld
()
{
std
::
cout
<<
"Task defined as regular (static) function
\n
"
;
}
void
workersTest
(
int
threads
)
{
jlworkers
::
Workers
workerThread
(
threads
);
std
::
mutex
printMutex
;
/*workerThread.post([]() {
std::cout << "Hello, World!\n";
});*/
workerThread
.
post
(
helloWorld
);
/* Basic tests
*/
void
tests
()
{
/* Default constructor uses one thread, acting as an event loop.
*/
jlworkers
::
Workers
eventLoop
,
workerThreads
(
4
);
workerThread
.
start
();
/* Start both workers. They'll stand by and wait for tasks.
*/
eventLoop
.
start
();
workerThreads
.
start
();
w
or
k
er
Thread
.
post
([]()
{
// Let's delay this a bit
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
500
));
std
::
cout
<<
"
Anonymous function with 500ms sleep before print
\n
"
;
/* Tasks A and B can run in any
or
d
er
*/
workerThreads
.
post
([]
{
std
::
cout
<<
"
Task A
\n
"
;
});
for
(
int
i
=
0
;
i
<
10
;
i
++
)
workerThread
.
post
([
i
]
{
std
::
cout
<<
"Task "
<<
i
<<
", thread id "
<<
std
::
this_thread
::
get_id
()
<<
"
\n
"
;
workerThreads
.
post
([]
{
std
::
cout
<<
"Task B
\n
"
;
});
workerThread
.
join
();
}
void
stuff
()
{
std
::
atomic_bool
wait
(
true
);
std
::
mutex
mtx
;
std
::
condition_variable
cv
;
std
::
thread
t
([
&
wait
,
&
mtx
,
&
cv
]
{
//while (wait);
std
::
unique_lock
<
std
::
mutex
>
lock
(
mtx
);
while
(
wait
)
cv
.
wait
(
lock
);
std
::
cout
<<
"Thread finished"
<<
std
::
endl
;
/* Task C shall always run before task D
*/
eventLoop
.
post
([]
{
std
::
cout
<<
"Task C
\n
"
;
});
eventLoop
.
post
([]
{
std
::
cout
<<
"Task D
\n
"
;
});
// std::this_thread::sleep_for(std::chrono::seconds(1));
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mtx
);
wait
=
false
;
}
cv
.
notify_all
();
t
.
join
();
}
void
timeoutTest
()
{
jlworkers
::
Workers
eventLoop
;
eventLoop
.
start
();
eventLoop
.
post_timeout
([]
{
std
::
cout
<<
"Hello
\n
"
;
},
1000
);
/* Tasks E and F will start after a timeout given in milliseconds
*/
eventLoop
.
post_timeout
([]
{
std
::
cout
<<
"Task E
\n
"
;
},
1500
);
eventLoop
.
post_timeout
([]
{
std
::
cout
<<
"Task F
\n
"
;
},
1000
);
/* Wait for all threads to finish
*/
eventLoop
.
join
();
workerThreads
.
join
();
}
int
main
(
int
argc
,
const
char
**
argv
)
{
// Test workerthread
workersTest
(
16
);
// Test eventloop
workersTest
(
1
);
// Test timeout
std
::
cout
<<
"Testing post_timeout
\n
"
;
timeoutTest
();
tests
();
return
0
;
}
This diff is collapsed.
Click to expand it.
workers.cpp
+
17
−
0
View file @
81a5a218
/*
* Copyright (C) 2021 Joakim Skogø Langvand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
<chrono>
#include
<condition_variable>
#include
<cstdlib>
...
...
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