简体   繁体   中英

Run on several cores

Initially, I have a program, which I divided into several parts so that each part is executed by a specific core. So, in my C++ project I have several "main files". I would like to know if it is possible from Visual Studio 2017 to say "Such core executes such.cpp".

Using the simple example of a counter and a display: The counter turns on core 1 and sends its data to the display on core 2. Is this possible to run on Visual Studio 2017?

No, this is not possible, and in fact it would be pointless.

In your simple idea, core2 has nothing to do until core1 sends it some data, at which point core1 would be waiting for core2. So at most one of the two cores is active at any time. It would be far more efficient to use one core for that.

To use multiple cores in C++, you need <thread> . Using <thread> is anything but automatic. However, once you have threads, using multiple cores is automatic.

There is a call in Windows that can limit your process to specific core:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setprocessaffinitymask

In this case you have to launch multiple proceses, set their affinities and do different task in each one.

And there is also one for threads:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setthreadaffinitymask

In a simple case you would launch new thread for each of your task, set their affinities, run tasks in threads and then wait for threads to join with your main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM