简体   繁体   中英

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).

Now there is sched_setaffinity function to set affinity for a given 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.

Ok, I got the answer.

One way to do this is like that:

  1. Create a new thread using 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.
  4. The POSIX timer thread, as well as threads created when the timer overflows, will inherit affinity from our thread created in step 1.
  5. Join the thread from step 1 with the main thread. This thread will cease to exist, but its child thread (timer) will persist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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