简体   繁体   English

如何在Linux下的C / C ++中为计时器设置亲和力?

[英]How to set affinity for a timer in C/C++ under Linux?

I have timer created with a function timer_create(CLOCK_REALTIME, &events, &timer) , where timer is a unique per process timer ID (according to man). 我已经使用功能timer_create(CLOCK_REALTIME, &events, &timer)创建了timer_create(CLOCK_REALTIME, &events, &timer) ,其中timer是每个进程的唯一计时器ID(根据man)。

Now there is sched_setaffinity function to set affinity for a given PID. 现在有sched_setaffinity函数来设置给定PID的亲和力。

How can I set affinity for my timer? 如何设置计时器的亲和力? Not the whole program, but only the timer thread. 不是整个程序,而是定时器线程。 I am not sure that timer ID is the same thing as PID. 我不确定计时器ID是否与PID相同。

Ok, I got the answer. 好,我知道了。

One way to do this is like that: 一种方法是这样的:

  1. Create a new thread using pthread. 使用pthread创建一个新线程。
  2. Set affinity for this new thread. 为此新线程设置亲和力。
  3. In the entry routine of the new thread, initialize the timer (create and set). 在新线程的输入例程中,初始化计时器(创建和设置)。 We can use SIGEV_THREAD as it is convenient to use. 我们可以使用SIGEV_THREAD,因为它使用起来很方便。
  4. The POSIX timer thread, as well as threads created when the timer overflows, will inherit affinity from our thread created in step 1. POSIX计时器线程以及计时器溢出时创建的线程将继承步骤1中创建的线程的相似性。
  5. Join the thread from step 1 with the main thread. 将步骤1中的线程与主线程连接。 This thread will cease to exist, but its child thread (timer) will persist. 该线程将不复存在,但其子线程(计时器)将保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM