如何确保在多核中创建std :: thread? [英] How to ensure that std::thread are created in multi core?
问题描述
我使用visual studio 2012.我有一个模块,在那里,我必须从硬盘读取一大组文件,穿过它们相应的路径通过xml。为此我正在做
std :: vector< std :: thread> m_ThreadList;
在while循环中,我将一个新线程推回到这个向量中,例如
m_ThreadList.push_back(std :: thread(& MyClass :: Readfile,& MyClassObject,filepath,std :: ref(polygon)) );
我的C ++ 11多线程知识是有限的。我在这里的问题是,做在特定的核心上创建线程?我知道在vs2012中的parallel_for和parallel_for_each,使最佳使用的内核。但是,有没有办法这样做使用标准的C ++ 11?
正如其他评论指出,在特定核心上创建线程,因为C ++不知道这样的架构细节。此外,在大多数情况下,操作系统将能够足够好地管理核心/处理器之间的线程分布。
也就是说,存在这样的情况,其中强制在核心之间的线程的特定分布可以有益于性能。作为示例,通过强制线程在一个特定核上执行,可以最小化不同处理器缓存之间的数据移动(这对于在某些存储器限制情况下的性能是至关重要的)。
如果你想走这条路,你必须看看平台特定的例程。例如,对于带有POSIX线程的GNU / linux,您将需要在FreeBSD cpuset_setaffinity()
中的 pthread_setaffinity_np()
SetThreadAffinityMask()
等。
如果你有兴趣,我有一些相关的代码片段:
http:// gitorious。 org / piranhapp0x / mainline / blobs / master / src / thread_management.cpp
I am using visual studio 2012. I have a module, where, I have to read a huge set of files from the hard disk after traversing their corresponding paths through an xml. For this i am doing
std::vector<std::thread> m_ThreadList;
In a while loop I am pushing back a new thread into this vector, something like
m_ThreadList.push_back(std::thread(&MyClass::Readfile, &MyClassObject, filepath,std::ref(polygon)));
My C++11 multi threading knowledge is limited.The question that I have here , is , how do create a thread on a specific core ? I know of parallel_for and parallel_for_each in vs2012, that make optimum use of the cores. But, is there a way to do this using standard C++11?
As pointed out in other comments, you cannot create a thread "on a specific core", as C++ has no knowledge of such architectural details. Moreover, in the majority of cases, the operating system will be able to manage the distribution of threads among cores/processors well enough.
That said, there exist cases in which forcing a specific distribution of threads among cores can be beneficial for performance. As an example, by forcing a thread to execute onto a one specific core it might be possible to minimise data movement between different processor caches (which can be critical for performance in certain memory-bound scenarios).
If you want to go down this road, you will have to look into platform-specific routines. E.g., for GNU/linux with POSIX threads you will want pthread_setaffinity_np()
, in FreeBSD cpuset_setaffinity()
, in Windows SetThreadAffinityMask()
, etc.
I have some relevant code snippets here if you are interested:
http://gitorious.org/piranhapp0x/mainline/blobs/master/src/thread_management.cpp
这篇关于如何确保在多核中创建std :: thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!