Skip to content

This PR adds a new class to manage threads - #11

Open
juanjqo wants to merge 8 commits into
SmartArmStack:jazzyfrom
juanjqo:jazzy_loopancho
Open

This PR adds a new class to manage threads #11
juanjqo wants to merge 8 commits into
SmartArmStack:jazzyfrom
juanjqo:jazzy_loopancho

Conversation

@juanjqo

@juanjqo juanjqo commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Hi @mmmarinho,

This PR adds the class sas::thread_manager (codename: loopancho) to manage threads. The main motivation is the standardisation of the SAS drivers (at least in the Adorno-lab::RAICo projects), which currently implement custom threads. This class is experimental, since I haven't tested it yet with real platforms. It is the first version and we can improve it.

Current features:

  • Based on modern C++ and the standard library (no Boost =) )
  • Support for priority levels (only for GNU/Linux)
  • The class relies on sas::Clock for time computations.
  • Thread-safety

Please let me know what you think and whether the design aligns with what you have in mind.

Minimal example

#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;
}

CMake

cmake_minimum_required(VERSION 3.16)

project(minimal_example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
    sas_core
    GIT_REPOSITORY https://github.com/juanjqo/sas_core.git
    GIT_TAG        jazzy_loopancho
)
set(ROS2_BUILD OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(sas_core)
add_executable(minimal_example main.cpp)
target_link_libraries(minimal_example PRIVATE sas_core_pure)

output

./minimal_example
**************************************************************************
sas::Clock (c) Murilo M. Marinho (murilomarinho.info) 2016-2026 LGPLv3
**************************************************************************
1
2
3
4
5
6
7
8
9
10
11
17:05:20: The command "/home/juanjqo/Documents/sas_core_test_examples/thread_manager/minimal_example/build/Desktop_Qt_6_10_1-Debug/minimal_example" finished successfully.

Kind regards,

Juancho

@mmmarinho mmmarinho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the submission. Some suggestions!

namespace sas
{

class thread_manager

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class names are PascalCase I think?



// Performance monitoring using sas::Clock
double get_computation_time() const;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend adding a get_clock() method that returns a const& so that you don't have to copy all the methods and make this a bit easier to maintain.

void thread_manager::stop()
{
if (!running_.exchange(false)) {
return; // Not running

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you don't need to return early but I'm happy to be corrected. Isn't it ok to always check if it's joinable?

* @details The method maps the abstract PRIORITY levels to specific Linux
* scheduler policies and priority values:
*
* | PRIORITY Level | Linux Policy | Priority Value | Description |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific source for this or an original thought? Either way it's fine, just curiosity.

* control.start();
* @endcode
*/
void thread_manager::apply_cpu_affinity() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants