这个pthread实际上是如何工作的?
我实际上正在使用 SMP 进行编译器项目,并且想要使用 pthreads 进行编码,并且听说过许多并行的东西打开 mpi 等等,因此首先从调用 pthread 时如何将该线程分配给核心开始,有没有什么方法可以给出通过pthreads将线程分配到不同的核心?
I am actually on my project on compiler with SMP, and want to code with pthreads and heard about many parallel things open mpi and so on, So to start with how this thread is allocated to core while calling pthread,Is there any way to give threads to different cores by pthreads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 pthreads 教程,例如此。但总的来说,我建议使用比 pthreads 更高级别的库,因为使用它们会更简单/更系统。例如,Boost::Threads 或英特尔 TBB。
Check out a pthreads tutorial such as this. But in general I would recommend using a higher level library than pthreads as it will be simpler/more systematic to use them. For example, Boost::Threads or Intel TBB.
系统会将线程分配给可用的核心,并在其他地方有空闲资源时将它们移动。
操作系统在分配资源方面通常比我做得更好,但如果你真的愿意,大多数操作系统都有一种将线程分配/限制到特定核心/处理器的方法。在 Linux 上,使用 sched_setaffinity
The system will assign the threads to available cores, and move them around if there's free resources elsewhere.
The OS usually does a far better job than me in assigning resources, but if you really want to, most OSs have a way of assigning/restricting threads to a particular core/processors. On linux, use sched_setaffinity
最简单的答案是肯定的——一般来说,不同的线程可能会自动在不同的内核上运行,而无需指定线程运行的位置。
在深入研究特定的线程库/思维方式之前,您似乎需要对多线程编程进行一般性的介绍/入门。在执行其他操作之前,请阅读上面 Amit 的“什么是线程”部分,然后开始查看 POSIX 线程、Windows 线程、TBB、Boost 线程或任何其他您感兴趣的库/包装器的细节。
The simplest answer is yes -- in general different threads will likely run on different cores, automatically without having to specify where the threads run.
It seems like you need an intro/primer on multi-threaded programming in general before diving into a specific threading library/way of thinking. Before doing anything else, read the section "What is a thread" from Amit above, then start looking at either the specifics of POSIX threads, Windows threads, TBB, Boost Threads, or any other library/wrapper looks interesting to you.