Bind threads to processors

前端 未结 2 1404
南笙
南笙 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.

提交回复
热议问题