01 ws ipc

This commit is contained in:
Robin Müller 2022-10-05 11:03:55 +02:00
parent 0e5443e598
commit 5e779dca6f
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
3 changed files with 51 additions and 5 deletions

View File

@ -54,6 +54,7 @@ It is recommended to have a basic understanding of C++ basics and object-oriente
in general before doing this workshop. There are various books and online resources available to in general before doing this workshop. There are various books and online resources available to
learn this. learn this.
All tasks are done in the `main.cpp` of this project and are located in subfolders.
In general, it is recommended to start with the `ws-tasks` workshop and then move to the In general, it is recommended to start with the `ws-tasks` workshop and then move to the
`ws-objects` workshop. `ws-objects` workshop.

View File

@ -0,0 +1,36 @@
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
std::mutex SHARED_LOCK;
static uint32_t SHARED_COUNTER = 0;
void task0() {
using namespace std::chrono_literals;
while (true) {
SHARED_LOCK.lock();
cout << "Task0: Shared counter after increment" << ++SHARED_COUNTER << endl;
SHARED_LOCK.unlock();
this_thread::sleep_for(1000ms);
}
}
void task1() {
using namespace std::chrono_literals;
while(true) {
{
std::lock_guard lg(SHARED_LOCK);
cout << "Task1: Shared counter after increment" << ++SHARED_COUNTER << endl;
}
this_thread::sleep_for(1000ms);
}
}
int main() {
std::thread t0(task0);
std::thread t1(task1);
t0.join();
t1.join();
}

View File

@ -1,5 +1,9 @@
# Threads and Tasks # Threads and Tasks
This workshop is split into 4 subtasks which are done in the `main.cpp` of this
project.
## Background Information
A satellite is a complex system which usually has a lot of tasks which need to be done A satellite is a complex system which usually has a lot of tasks which need to be done
simulatenously by a dedicated On-Board Computer (OBC). This can include for example: simulatenously by a dedicated On-Board Computer (OBC). This can include for example:
@ -36,10 +40,12 @@ string every second: "Hello World".
## 2. Changing to the concept of executable objects ## 2. Changing to the concept of executable objects
The goal of this task is to convert the code from task 1 so the `std::thread` API takes an The goal of this task is to convert the code from task 1 so the `std::thread` API takes an
executable object to move to a more object oriented task approach. The printout of the thread executable object by reference to move to a more object oriented task approach.
should remain the same. The executable objects should be named `MyExecutableObject`. It contains
one function called `periodicOperation` which performs the printout, and a static function which The printout of the thread should remain the same. The executable objects should be named
takes the `MyExecutableObject` itself by reference and executes it in a permanent loop. `MyExecutableObject`. It contains one function called `periodicOperation` which performs the
printout, and a static function which takes the `MyExecutableObject` itself by reference and
executes it in a permanent loop.
The executable object should be passed into the `std::thread` directly. The executable object should be passed into the `std::thread` directly.
@ -59,7 +65,10 @@ The executable object should be passed into the `std::thread` directly.
4. Implement `executeTask`. This function uses the passed object and performs the scheduling 4. Implement `executeTask`. This function uses the passed object and performs the scheduling
specific part by calling `self.performOperation` in a permanent loop with a delay between specific part by calling `self.performOperation` in a permanent loop with a delay between
calls. You can hardcode the delay to 1000ms for the first implementation. calls. You can hardcode the delay to 1000ms for the first implementation.
5. Add a constructor to `MyExecutableObject` which expects a millisecond delay 5. Change your `std::thread` calls in the main. You can pass the new `executeTask` function
as the executable unit. The second argument should be an instance of the executable object
itself. You might need the `std::reference_wrapper` to pass it as a reference.
6. Add a constructor to `MyExecutableObject` which expects a millisecond delay
as an `uint32_t` and cache it as a member variable. Then use this member as an `uint32_t` and cache it as a member variable. Then use this member
variable in the `executeTask` implementation to make the task frequency configurable via the variable in the `executeTask` implementation to make the task frequency configurable via the
constructor (ctor) parameter. constructor (ctor) parameter.