From 5e779dca6f93b2b0da39c0253e5565d3e8571465 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 5 Oct 2022 11:03:55 +0200 Subject: [PATCH] 01 ws ipc --- README.md | 1 + ws-ipc/ipc-solutions/main-01.cpp | 36 ++++++++++++++++++++++++++++++++ ws-tasks/README.md | 19 ++++++++++++----- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 ws-ipc/ipc-solutions/main-01.cpp diff --git a/README.md b/README.md index 46bdad2..9abc441 100644 --- a/README.md +++ b/README.md @@ -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 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 `ws-objects` workshop. diff --git a/ws-ipc/ipc-solutions/main-01.cpp b/ws-ipc/ipc-solutions/main-01.cpp new file mode 100644 index 0000000..86c3960 --- /dev/null +++ b/ws-ipc/ipc-solutions/main-01.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +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(); +} diff --git a/ws-tasks/README.md b/ws-tasks/README.md index 2da331f..a297bbe 100644 --- a/ws-tasks/README.md +++ b/ws-tasks/README.md @@ -1,5 +1,9 @@ # 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 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 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 -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 -takes the `MyExecutableObject` itself by reference and executes it in a permanent loop. +executable object by reference to move to a more object oriented task approach. + +The printout of the thread 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 takes the `MyExecutableObject` itself by reference and +executes it in a permanent loop. 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 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. - 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 variable in the `executeTask` implementation to make the task frequency configurable via the constructor (ctor) parameter.