Pinning a thread to a core in a cpuset through C

前端 未结 2 1282
慢半拍i
慢半拍i 2021-02-14 05:11

I have /cgroup/cpuset/set1. set1 has 2-5,8. I want to bind a process to that cpuset and then pin a thread in that process to, say, core 4. The name of the cpuset and the thread

2条回答
  •  悲哀的现实
    2021-02-14 05:33

    Call the following Function and pass whatever core ID you want to pass. Also from wherever you call this function do check its return value to be 1.

    short CorePin(int coreID)
    {
      short status=0;
      int nThreads = std::thread::hardware_concurrency();
      //std::cout< nThreads)
      {
        std::cout<<"Invalid CORE ID"<<"\n";
        return status;
      }
    
      CPU_SET(coreID,&set);
      if(sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
      {
        std::cout<<"Unable to Set Affinity"<<"\n";
        return -1;
      }
      return 1;
    }
    

提交回复
热议问题