I have a Raspberry Pi 4, running the latest Raspberry Pi OS version. I have a C++ program running on the Raspberry Pi and i want to distribute the execution of this program on cores 1+2+3, while Raspberry Pi OS runs on core 0.

Down below is a picture of the htop-command:

enter image description here

As you can see, my program is relatively CPU intensive, which is why I want to distribute the execution of the program on core 1+2+3 (core 2+3+4 on the picture above). Core 0 (core 1 on the picture above) runs Raspberry Pi OS. Right now, the execution of my C++ program is only happening on core 2, and I don't know why...

Below is the systemd startup-service:

Description=Automatically start software

ExecStart=sudo ./path/to/program
CPUAffinity=1 2 3


The program is set to highest priority (nice=-20) and i'm using CPUAffinity=1 2 3 to let core 1+2+3 work on the program.

Below i the cmdline.txt file:

console=serial0,115200 console=tty1 root=PARTUUID=97f8b39e-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles isolcpus=1,2,3

Here I isolate CPU core 1+2+3.

Below is a response when i run the taskset -cp<PID>-command:

pid 651's current affinity list: 1-3

The affinity list is correct = core 1-3.

My problem: When running the htop command, the program is still only executed on core 1 (core 2 in htop). How do I distribute the execution of the program so it is run on core 1+2+3?

If i run the exact same program, with the same setup in the systemd startup-service and cmdline.txt files, but on a dietPi OS, the workload of the program is distributed among core 1+2+3, where core 0 runs the dietPi OS:

enter image description here

Here, the workload per core is ~30%, and in Raspberry Pi OS it is ~90% on core 1... (core 2 in the first htop picture).


1 Answer 1


Check your program is actually multi-threaded enough to run in parallel.

(Also you shouldn't need the sudo - IIRC systemd will run your command as root by default)

  • My program only has 1 main thread. And the startup-service doesn't work if I omit sudo
    – jrn6270
    Commented Jun 24, 2020 at 11:27
  • 3
    If your program only has 1 computing thread, it can only use one core. Commented Jun 24, 2020 at 13:39
  • The program is written using C++. But how can dietPi then use 3 cores on the same program?
    – jrn6270
    Commented Jun 24, 2020 at 13:46
  • 2
    DietPi is an operating system, just as Raspberry OS is. So it has complete control of all the hardware. What I suspect is happening is that it is switching the cores, only one core at a time is running your code but because it switches quick enough it appears to be using all the cores.
    – Nick
    Commented Jun 24, 2020 at 14:18
  • Does running your program on DietPi actually have 3x the through-put? If it takes about the same time for given input, then it might just be the way it's being reported. If you are computing on one thread, only one CPU core can be used. Commented Jun 24, 2020 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.