Bind threads to processors

前端 未结 2 1394
南笙
南笙 2020-12-31 19:45

When I run my multi-threaded code, the system (linux) sometimes moves the threads from one processor to another. As I have as many threads as I have processors, it invalidat

相关标签:
2条回答
  • 2020-12-31 20:09

    Use sched_setaffinity (this is Linux-specific).

    Why would a scheduler switch threads between different processors? Well, imagine that your thread last ran on processor 1 and is currently waiting to be scheduled for execution again. In the meantime, a different thread is currently running on processor 1, but processor 2 is free. In this situation, it's reasonable for the scheduler to switch your thread to processor 2. However, a sophisticated scheduler will try to avoid "bouncing" a thread between processors more than necessary.

    0 讨论(0)
  • 2020-12-31 20:11

    You can do this from bash. There is a wonderful taskset command I acquainted in this question (you may also find valuable discussion on how scheduler should operate there). The command takes a pid of a process and binds it to the specific processor(s).

    taskset -c 0 -p PID
    

    binds the process with PID to processor (core) number 0.

    What does it have to do with threads? To each thread is assigned an identifier with the same rights as pid, also known as "tid". You can get it with gettid syscall. Or you can watch it, for example, in top program by pressing H (some processes will split to many seemingly equal entries with different pids---those are threads).

    0 讨论(0)
提交回复
热议问题