Is it normal that the CPU binded to a thread changes randomly after sched_set_affinity?

57 Views Asked by At

Android, I created 8 threads and binded them to 8 different CPU cores. The binding was successful. However, after the threads running for a while, the CPU affinity seemed invalid. The CPU binded to a thread changed randomly. Is it normal or a bug?

the cpu affinity setting code is like this:

for (int i=0; i<8; ++i){
    mWorkers.push_back([i]()  {
            cpu_set_t cpuset;
            CPU_ZERO(&cpuset);
            CPU_SET(i, &cpuset);
            pid_t tid = pthread_gettid_np(mWorkers[i-1].native_handle());
            int rc = sched_setaffinity(tid, sizeof(cpu_set_t), &cpuset);
            std::this_thread::sleep_for(std::chrono::milliseconds(20));
            while (!mStop) {
                // some code ..
            }
        } );
}
0

There are 0 best solutions below