How to execute system command in parallel in c++

谁都会走 提交于 2019-12-24 00:39:59

问题


Normally when I want to run a executable from a c++ code. I just use the code:

system("path\to\the\executable param"); 

Now, I want to run the executable file in parallel. I use 2 threads. The first thread will call:

system("path\to\the\executable param1");

The second thread will call:

system("path\to\the\executable param2");

However it doesn't run in parallel as I expect.

Is there in way to solve this?


回答1:


You can run multiple commands as below:

system("path\\to\\the\\executable param1 &");
system("path\\to\\the\\executable param2");

This way both will run in parallel, and your program doesn't need to be multi-threaded for this.




回答2:


  • For windows: CreateProcess (see MSDN) function.
  • For *NIX: first, spwan a child with fork, then replace a child code with execXX (execl, execle, execlp, execv, execvp).


来源:https://stackoverflow.com/questions/23595141/how-to-execute-system-command-in-parallel-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!