-
Notifications
You must be signed in to change notification settings - Fork 3
This PR adds a new class to manage threads #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juanjqo
wants to merge
8
commits into
SmartArmStack:jazzy
Choose a base branch
from
juanjqo:jazzy_loopancho
base: jazzy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+593
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04fa52f
Added a new class to manage threads in C++
juanjqo 4481d27
[cpplib.cmake] added the thread_manager
juanjqo 814a39f
Added an example to test the thread manager
juanjqo d16d765
Fixed the format in the header
juanjqo 3086c77
removed redundant pragma once
juanjqo 1f6d1c1
Added missing copyright message
juanjqo ce3b324
Added support for thread priorities for linux systems
juanjqo 5ed93e8
updated the documentation of the ctor
juanjqo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| # Copyright (c) 2022-2026 Murilo Marques Marinho | ||
| # | ||
| # This file is part of sas_core. | ||
| # | ||
| # sas_core is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Lesser General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # sas_core 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 Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with sas_core. If not, see <https://www.gnu.org/licenses/>. | ||
| # | ||
| # ################################################################ | ||
| # | ||
| # Author: Juan Jose Quiroz Omana, email: juanjose.quirozomana@manchester.ac.uk | ||
| # | ||
| # ################################################################*/ | ||
|
|
||
| #pragma once | ||
| #include <sas_core/sas_core.hpp> | ||
| #include <sas_core/sas_clock.hpp> | ||
| #include <functional> | ||
| #include <thread> | ||
| #include <atomic> | ||
| #include <string> | ||
|
|
||
| namespace sas | ||
| { | ||
|
|
||
| class thread_manager | ||
| { | ||
| public: | ||
| // PRIORITY levels | ||
| enum class PRIORITY { | ||
| LOWEST = 0, | ||
| BACKGROUND = 10, | ||
| NORMAL = 50, | ||
| HIGH = 80, | ||
| REALTIME = 90, | ||
| CRITICAL = 99 | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| private: | ||
| // Thread optimization parameters | ||
| PRIORITY priority_{PRIORITY::NORMAL}; | ||
| int cpu_core_{-1}; | ||
| void apply_priority(); | ||
| void apply_cpu_affinity(); | ||
|
|
||
|
|
||
| protected: | ||
| sas::Clock clock_; | ||
| std::function<void()> loop_callback_; | ||
| std::thread thread_; | ||
| std::string thread_name_; | ||
| std::atomic<bool> running_{false}; | ||
| std::atomic<bool> stop_requested_{false}; | ||
|
|
||
| protected: | ||
| void run(); | ||
| public: | ||
| ~thread_manager(); | ||
|
|
||
| thread_manager(const thread_manager&) = delete; // Copy constructor | ||
| thread_manager& operator=(const thread_manager&) = delete; // Copy assignment | ||
| thread_manager(thread_manager&&) = delete; // Move constructor | ||
| thread_manager& operator=(thread_manager&&) = delete; // Move assignment | ||
|
|
||
| thread_manager(const std::string& thread_name, | ||
| const double& period, | ||
| std::function<void()> callback, | ||
| PRIORITY priority = PRIORITY::NORMAL, | ||
| int cpu_core = -1); | ||
|
|
||
|
|
||
|
|
||
| // Performance monitoring using sas::Clock | ||
| double get_computation_time() const; | ||
|
juanjqo marked this conversation as resolved.
|
||
| double get_sleep_time() const; | ||
| double get_effective_sampling_time() const; | ||
| double get_elapsed_time_sec() const; | ||
| long get_overrun_count() const; | ||
|
|
||
| // Statistics from sas::Clock | ||
| double get_statistics(const Statistics& statistics, | ||
| const sas::Clock::TimeType& time_type) const; | ||
|
|
||
| // Lifecycle | ||
| void start(); | ||
| void stop(); | ||
| bool is_running() const; | ||
|
|
||
|
|
||
| // Getters | ||
| std::string get_thread_name() const; | ||
| PRIORITY get_priority() const; | ||
| int get_cpu_core() const; | ||
| double get_period() const; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| # Copyright (c) 2016-2026 Murilo Marques Marinho | ||
| # | ||
| # This file is part of sas_core. | ||
| # | ||
| # sas_core is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Lesser General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # sas_core 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 Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with sas_core. If not, see <https://www.gnu.org/licenses/>. | ||
| # | ||
| # ################################################################ | ||
| # | ||
| # Author: Juan Jose Quiroz Omana (juanjose.quirozomana@manchester.ac.uk) | ||
| # | ||
| # ################################################################*/ | ||
|
|
||
| #include <sas_core/sas_thread_manager.hpp> | ||
| #include <iostream> | ||
| #include <thread> | ||
|
|
||
| int main() { | ||
| int counter = 0; | ||
|
|
||
| // Create and start thread | ||
| sas::thread_manager tm("test", 0.1, [&counter]() { | ||
| std::cout << ++counter << "\n"; | ||
| }); | ||
|
|
||
| tm.start(); | ||
|
|
||
| // Run for 1 second | ||
| std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
|
|
||
| // Clean up | ||
| tm.stop(); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.