cpp-ru / ideas

Идеи по улучшению языка C++ для обсуждения
https://cpp-ru.github.io/proposals
Creative Commons Zero v1.0 Universal
89 stars 0 forks source link

*affinity* support #302

Open apolukhin opened 3 years ago

apolukhin commented 3 years ago

Перенос предложения: голоса +4, -1 Автор идеи: void78

Currently C++ is not aware of set/get affinity calls. Given the fact C++ has threads' support out of the box it makes sense to make one step ahead and think about adding that functionality.

Sometime it's crucial for the application to handle incoming events as much as possible. One of the possible way for that might be 'stealing' CPU core from the OS so does not participate in scheduling and bind your thread to that core. Sure we can always make kind of cpu binder:

class ThreadCpuBinder
{
public:
   ThreadCpuBinder(int cpu)
   {
      if(cpu != -1)
      {
          cpu_set_t cpuSet;

          CPU_ZERO(&cpuSet);
          CPU_SET(cpu, &cpuSet);

          if(pthread_setaffinity_np(pthread_self(), sizeof(cpuSet), &cpuSet))
          {
              // error indication
          }
      }
   }
};

Hope you've got enough with that) As for me I'd prefer to see 'cpu_set_t' class/struct (or whatever it will be called) which contains CPUs out thread/task/async is allowed to use. Implementing that sort of class has one important property - backward compatibility: in case you do not care about affinity you can simple ignore that parameter on creating stage.

apolukhin commented 3 years ago

yndx-antoshkka, 13 июня 2018, 12:14 Появилось предложение на эту тему http://wg21.link/p0796 , но оно сильно привязано к Executors, которые в C++20 скорее всего не попадут.