Reputation: 189
I have written a program in C, and I just want only one function running in another processor core.
Does anyone know how to distribute threads on different cores? Can a single running thread running on one core, can sometimes utilize another core as well, depending on how busy the cores are?
Upvotes: 4
Views: 2370
Reputation: 4019
I think you are asking how do threads get distributed to run on different processors in a multi-processor CPU? If so, my understanding is that this is something that is controlled by the operating system. Take a look at this SO question.
Upvotes: 2
Reputation: 10393
I believe you are looking for set affinity functionality.
Processor affinity is a modification of the native central queue scheduling
algorithm in a symmetric multiprocessing operating system. Each task
(be it process or thread) in the queue has a tag indicating its
preferred / kin processor. At allocation time, each task is allocated to
its kin processor in preference to others.
Check out this thread which discusses the usage of setting affinity to particular thread. Excerpts from this thread:
int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);
Passing 0 as the pid, and it'll apply to the current thread only, or have other
thread report their kernel pid with the linux specific call pid_t gettid(void);
and pass that in as the pid.
Upvotes: 6