# Learning how to lock a thread to a CPU core

Source: https://forum.modalai.com/topic/3799/learning-how-to-lock-a-thread-to-a-cpu-core
Category: General Questions (https://forum.modalai.com/category/2/general-questions)
Tags: cpu, resource-aware, threads
Posted: 2024-09-13 16:54:23 UTC by zauberflote1 (ModalAI staff)
Replies: 0 · Views: 1274

## zauberflote1 (ModalAI staff) · 2024-09-13 16:54:23 UTC

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
