多核处理器中的线程

4
我已经用C语言编写了一个程序,我只想在另一个处理器内核中运行一个函数。
有人知道如何将线程分配到不同的内核上吗?单个正在运行的线程能否在一个内核上运行,根据内核的繁忙程度,有时也可以利用另一个内核?

6
这句话通过某种机器翻译吗? - Hasturkun
2个回答

6

我相信您正在寻找设置亲和性的功能。

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.

请查看这个帖子,其中讨论了将亲和力设置为特定线程的用法。 以下是该帖子的摘录:

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.

2

我认为你在询问多处理器CPU中如何分配线程在不同处理器上运行。如果是这样,我的理解是这是由操作系统控制的。请查看这个SO问题


首先感谢您的思考。但是,我不想与.NET并行,而是在Linux下使用C,但是没有API或C库!!! - farka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接