vad
vad

Reputation: 344

Assign a function to a CPU

In the Linux kernel, long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) is used to assign a function running on a specific CPU core. Is there any equivalent user-space function?

Upvotes: 2

Views: 197

Answers (1)

nos
nos

Reputation: 229088

No, there is not, the execution contexts provided by the kernel are quite different than that of the user space.

What you can do is.

  1. pthread_create() a new thread.

  2. Pin that thread to a particular CPU, with pthread_setaffinity_np()

If you need a similar API to what the kernel have, you need to create a small pool of worker threads, one per CPU, using the above approach, and create the necessary plumbing code to be able to request functions to be called on one of those worker threads.

Upvotes: 5

Related Questions