title: Parallel calling of an external program in C++
body: <p>As in topic I have a problem with parallel calling of external program in C++. I prepared code showed below that uses system() to call my program but it is not thread safe and results are random - some runs are fine other return errors. Is there any other possible method to run it parallel in thread safe mode? For now I use bat file with multiple start commands and wait when all threads return output files, but it is very ugly solution.</p>
  
  <pre><code>vector&lt;thread&gt; threadArray;
  
      int nThread= 0;
  
      // run calcs
  
      for (int nThread = 0; nThread &lt; quantity; nThread++)
      {
          threadArray.push_back(thread(system, ("start ccx.exe Calculix_wings_" + to_string(nThread)).c_str()));
          threadArray.push_back(thread(system, ("start ccx.exe Calculix_fus_" + to_string(nThread)).c_str()));
          cout &lt;&lt; ("start ccx.exe Calculix_wings_" + to_string(nThread)).c_str() &lt;&lt; endl;
          cout &lt;&lt; ("start ccx.exe Calculix_fus_" + to_string(nThread)).c_str() &lt;&lt; endl;
      }
  
      for (auto it = threadArray.begin(); it != threadArray.end(); ++it)
      {
          it-&gt;join();
      }
  </code></pre>
  
  <p>I have found some simmilar topics but none of them was helpful.</p>
  
  <p>The program is a structrural stress solver Calculix. Here is cmd output:</p>
  
  <pre><code>start ccx.exe Calculix_wings_0
  start ccx.exe Calculix_fus_0
  start ccx.exe Calculix_wings_1
  start ccx.exe Calculix_fus_1
  start ccx.exe Calculix_wings_2
  start ccx.exe Calculix_fus_2
  start ccx.exe Calculix_wings_3
  start ccx.exe Calculix_fus_3
  start ccx.exe Calculix_wings_4
  start ccx.exe Calculix_fus_4
  start ccx.exe Calculix_wings_5
  start ccx.exe Calculix_fus_5
  'üÜ+' is not recognized as an internal or external command,
  operable program or batch file.
  'Čŕ+' is not recognized as an internal or external command,
  operable program or batch file.
  '-' is not recognized as an internal or external command,
  operable program or batch file.
  'c' is not recognized as an internal or external command,
  operable program or batch file.
  'l' is not recognized as an internal or external command,
  operable program or batch file.
  'o' is not recognized as an internal or external command,
  operable program or batch file.
  'x' is not recognized as an internal or external command,
  operable program or batch file.
  'îţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţî
  ţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţîţî
  ţîţîţîţîţîţîţîţîţ' is not recognized as an internal or external command,
  operable program or batch file.
  '-' is not recognized as an internal or external command,
  operable program or batch file.
  'l' is not recognized as an internal or external command,
  operable program or batch file.
  </code></pre>
  
  <p>There must be a problem with thread safety as you can see in output. At the beginning there lines with start command - not exetutable, and then the errors related with system() in parallel run. In this particular run only two calculations finished with succeed, with '2' at the end. Hovewer this is random, sometimes 5 runs is done, sometimes 3. When I create from first lines a bat file everything is ok. I expect to run ccx.exe for every input and then wait till all threads finish calculations to further postprocessing output files.</p>