Skip to content

Instantly share code, notes, and snippets.

@yamamaya
Last active June 13, 2022 05:24
Show Gist options
  • Save yamamaya/26694faee1bedcd92013857111bfda92 to your computer and use it in GitHub Desktop.
Save yamamaya/26694faee1bedcd92013857111bfda92 to your computer and use it in GitHub Desktop.
Dedicate a cpu core to specified thread, in Raspberry Pi.
  1. At first, run this command by superuser. This will assign CPU cores other than #3 for all processes. But it will cause some errors, I'm not sure it is really perfect.
    ps -e -o pid= | sudo xargs -n 1 taskset -p -a 0xFFFFFFF7 2> /dev/null > /dev/null
  1. And then, run those codes in your program. This will assign CPU core #3 exclusively for the thread.
    // Get current thread ID
    pthread_t tid = pthread_self();
    
    // Set the CPU affinity mask of the thread
    cpu_set_t cpuset;
    CPU_ZERO( &cpuset );
    CPU_SET( 3, &cpuset );
    pthread_setaffinity_np( tid, sizeof( cpuset ), &cpuset );
    
    // Set  the  scheduling parameters of the thread
    struct sched_param params;
    params.sched_priority = sched_get_priority_max(SCHED_FIFO);
    pthread_setschedparam( tid, SCHED_FIFO, &params );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment