Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Collapse
Brand Logo

ModalAI Forum

  1. ModalAI Support Forum
  2. Ask your questions right here!
  3. Learning how to lock a thread to a CPU core

Learning how to lock a thread to a CPU core

Scheduled Pinned Locked Moved Ask your questions right here!
cpuresource-awarethreads
1 Posts 1 Posters 918 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • zauberflote1Z Offline
    zauberflote1Z Offline
    zauberflote1
    ModalAI Team
    wrote on last edited by
    #1

    Hello,
    If you are developing your own custom pipelines, applications, and modifying existing source code from ModalAI, this may be applicable to you.

    Motivation: Control which CPU core is responsible for a certain thread in your custom application.

    Background: It all started when I was observing the htop behavior. Sometimes the dynamic core allocation, overloads a particular core instead of keeping the effort evenly distributed, potentially causing issues if you have a "real-time" sensitive application. -- Assuming you have already set the priority and scheduler properly...

    Tackling the issue: If you look in the qvio-server source files, you will find that a particular computer vision thread is locked to CPU core 7 -- known to be the fastest VOXL2 core. Then, what if we use the same idea in a custom application (potentially for a different core), would it work? YES.

    How to do it:
    PS1: I rather use ROS over modalpipes, so the full example linked is a for a modified IMU server that controls the driver entirely via ROS, but you can do the same for a modalpipe implementation WLOG.

    Using the heavenly chosen language, C++, and ROS syntax

    void _set_affinity_to_core_7() {
        cpu_set_t cpuset;
        pthread_t thread = pthread_self();
    
        //SET IT UP TO CORE 7
        CPU_ZERO(&cpuset);
    //change this to the core you want
        CPU_SET(7, &cpuset); 
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset)) {
            perror("pthread_setaffinity_np");
        }
    
        //CHECK IF IT WAS SET UP
        if (pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset)) {
            perror("pthread_getaffinity_np");
        }
    
        ROS_INFO("IMU read thread is now locked to core: ");
        for (int j = 0; j < CPU_SETSIZE; j++) {
            if (CPU_ISSET(j, &cpuset)) {
                ROS_INFO(" %d", j);
            }
        }
        ROS_INFO("\n");
    }
    

    In the main() and using boost, define the lambda as follows:

        std::vector<boost::thread> threads;
        for (int i = 0; i < 1; ++i) {
            if (imu_enable[i]) {
                //IF YOU DONT WANT TO LOCK A SPECIFIC CORE, THEN 
                // threads.push_back(boost::thread(boost::bind(&readThreadFunc, i)));
    
                threads.push_back(boost::thread([i]() {
                _set_affinity_to_core_7();  
                readThreadFunc(i);
                }));
    
            }
        }
    

    PS2: Full Example can be found here:
    https://github.com/zauberflote1/voxlSyncVINS/blob/91d74b57cacd78db87c6bd48ac03f14ca36874ab/imu_ros/src/voxl_imu_ros/server/src/node.cpp#L43C32-L43C33

    Final Remarks:
    Only do this if you know what you are doing as you may overload a core by accident.

    All the best,
    ZBFT

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    ModalAI
    Categories Recent Tags ModalAI.com Docs
    © 2026 ModalAI® · Accelerating autonomy for smaller, smarter, safer drones · Powered by NodeBB
    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups