How to use multi-cpu in c ++?

I work in a lab and wrote a multi-threaded C ++ 11 computing program using std::thread . Now I have the opportunity to run my program on a server with multiple processors.

Server:

  • Starts Ubuntu Server
  • It has 40 Intel processors.

I don't know anything about programming with multiple processors. The first idea that comes to my mind is to launch 40 applications and then combine their results. It is possible, but I want to know more about my possibilities.

  • If I compile my code on the server using the gcc compiler, will the resulting application use multi-cpu?
  • Answer # 1 depends on how I can test it?

Thanks!

+7
c ++ multithreading c ++ 11 cpu cpu-usage
source share
2 answers

You ask a question not only about multithreading, but also about several processors.

  • Basically, the operating system automatically distributes threads among the cores. You do not have to do anything.

  • After you use C ++ 11, you have std::thread::get_id() , which you can call and identify another thread, but you CANNOT identify the kernel you are using. Use pthreads directly + "cpu affinity" for this.

For more information on how to gain control over it, you can use Google for “processor proximity”. If you need such accuracy. You can define the kernel, as well as select the kernel ... You can start with this: http://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html

0
source share

If your program is multithreaded, your OS should automatically ensure that it uses available processors.

Make sure that you distribute the work that you should do, about the same as there are processors that you can use. Make sure that this is not only one thread that does the work, but other threads are just waiting for this thread to complete.

+5
source share

All Articles