How to ensure that std :: thread is created in multi-core?

I am using visual studio 2012. I have a module where I need to read a huge set of files from the hard drive after passing their respective paths through xml. For this I do

std::vector<std::thread> m_ThreadList; 

In a while loop, I return a new thread to this vector, something like

 m_ThreadList.push_back(std::thread(&MyClass::Readfile, &MyClassObject, filepath,std::ref(polygon))); 

My multithreaded knowledge of My C ++ 11 is limited. The question I have here is how to create a thread on a specific kernel? I know about parallel_for and parallel_for_each in vs2012 which optimally use kernels. But is there a way to do this using standard C ++ 11?

+6
source share
3 answers

As pointed out in other comments, you cannot create a thread β€œon a specific core”, since C ++ does not know such architectural details. Moreover, in most cases, the operating system will be able to control the distribution of threads between cores / processors quite well.

However, there are cases where forced thread allocation between cores can be useful for performance. As an example, by forcing a thread to execute on one particular core, it would be possible to minimize the movement of data 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 need to study the platform routines. For example, for GNU / linux with POSIX threads, you need pthread_setaffinity_np() , in FreeBSD cpuset_setaffinity() , in Windows SetThreadAffinityMask() , etc.

I have some relevant code snippets if you are interested:

http://gitorious.org/piranhapp0x/mainline/blobs/master/src/thread_management.cpp

+3
source

I am sure that kernel affinity is not included in std :: thread. It is assumed that the OS is perfectly capable of making the best use of existing kernels. In all but the most extreme cases, you are not going to beat the OS solution, so the assumption is fair.

If you go this route, you need to add some kind of solution to your code in order to take into account the architecture of the machine, so that your solution is better than the OS on every computer that you run on. It takes a lot of effort! First you need to limit the number of threads according to the number of cores on the computer. And you do not know what else is going on in the car; OS is working!

This is why thread pools exist. By default, they have as many threads as there are kernels automatically configured by the language runtime. AFAIK C ++ 11 does not have one of them. Thus, one good thing you can do to get optimal performance is to find out how many cores there are and to limit the number of threads that you have on this number. Otherwise, it might be best to trust the OS.

Joachim Pileborg's comment is noteworthy unless the work performed by each thread outweighs the I / O overhead.

+2
source

As a brief overview of threads in the context of sending threads to kernels:

Most modern operating systems use kernel-level or hybrid threads. With a kernel level thread, the OS "sees" all threads in each process; unlike user-level threads, which are used in Java, where the OS sees one process and does not know the threads. Now, since the OS can recognize individual threads of a process during thread loading of the kernel level and controls their sending to the given kernel, there is a potential for true parallelism - where several threads of the same process are executed on different cores. You, as a programmer, will not control this, but when using std::thread ; OS decides. In user-level threading, flow control is done at the user level, and Java in the library controls the "send". In the case of hybrid streaming, kernel streaming is used, where each kernel thread is a collection of user-level threads.

0
source

All Articles