Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从c++;编程并等待两者都完成 我写了C++程序,在某个时候我需要两个程序的输出,这些程序是用动态定义的参数运行的。 目前,我运行两个外部程序,如下例所示: ... system("prog1 arg1".c_str()); system("prog2 arg2".c_str()); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ... ... void runextprog(string command){ system(command.c_str()) } ... vector < thread > threadVect; threadVect.clear(); threadVect.emplace_back(runextprog, "prog1 arg1"); threadVect.emplace_back(runextprog, "prog2 arg2"); threadVect[0].join(); threadVect[1].join(); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ..._C++_Multithreading - Fatal编程技术网

从c++;编程并等待两者都完成 我写了C++程序,在某个时候我需要两个程序的输出,这些程序是用动态定义的参数运行的。 目前,我运行两个外部程序,如下例所示: ... system("prog1 arg1".c_str()); system("prog2 arg2".c_str()); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ... ... void runextprog(string command){ system(command.c_str()) } ... vector < thread > threadVect; threadVect.clear(); threadVect.emplace_back(runextprog, "prog1 arg1"); threadVect.emplace_back(runextprog, "prog2 arg2"); threadVect[0].join(); threadVect[1].join(); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ...

从c++;编程并等待两者都完成 我写了C++程序,在某个时候我需要两个程序的输出,这些程序是用动态定义的参数运行的。 目前,我运行两个外部程序,如下例所示: ... system("prog1 arg1".c_str()); system("prog2 arg2".c_str()); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ... ... void runextprog(string command){ system(command.c_str()) } ... vector < thread > threadVect; threadVect.clear(); threadVect.emplace_back(runextprog, "prog1 arg1"); threadVect.emplace_back(runextprog, "prog2 arg2"); threadVect[0].join(); threadVect[1].join(); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadoutput2(); dosomething(data1, data2); } else { cerr << "error" << endl; exit(1); } ...,c++,multithreading,C++,Multithreading,这种方法的问题是,如果一个或两个外部程序由于任何原因失败,我将永远被锁定在while循环中。 为了避免此问题,我考虑使用两个线程,如以下示例中所示: ... system("prog1 arg1".c_str()); system("prog2 arg2".c_str()); if(output1_exist() && output2_exist()){ data1 = loadoutput1(); data2 = loadout

这种方法的问题是,如果一个或两个外部程序由于任何原因失败,我将永远被锁定在while循环中。 为了避免此问题,我考虑使用两个线程,如以下示例中所示:

...
system("prog1 arg1".c_str());
system("prog2 arg2".c_str());

if(output1_exist() && output2_exist()){
  data1 = loadoutput1();
  data2 = loadoutput2();
  dosomething(data1, data2);
} else {
  cerr << "error" << endl;
  exit(1);
}
...
...
void runextprog(string command){
  system(command.c_str())
}

...

vector < thread > threadVect;

threadVect.clear();
threadVect.emplace_back(runextprog, "prog1 arg1");
threadVect.emplace_back(runextprog, "prog2 arg2");

threadVect[0].join();
threadVect[1].join();

if(output1_exist() && output2_exist()){
  data1 = loadoutput1();
  data2 = loadoutput2();
  dosomething(data1, data2);
} else {
  cerr << "error" << endl;
  exit(1);
}
...
。。。
void runextprog(字符串命令){
系统(command.c_str())
}
...
向量<线程>线程向量;
threadVect.clear();
threadVect.emplace_back(runextprog,“prog1 arg1”);
threadVect.emplace_back(runextprog,“prog2 arg2”);
threadVect[0]。连接();
threadVect[1]。连接();
if(output1_exist()&&output2_exist()){
data1=loadoutput1();
data2=loadoutput2();
剂量测定法(数据1、数据2);
}否则{

cerr这能回答你的问题吗?嗯,我的链接可能是特定于unix的…你的解决方案似乎不坏…或者你需要找到另一种方法来检测程序失败…一个超时时间左右。嗨@JHBonarius,谢谢你抽出时间。我正在查看你的链接,但它似乎是特定于c和*nix的答案。它应该可以工作,但如果有人SE有一个更便携的和更简单的C++习惯用法。如果你需要移植,你可能想看看。谢谢你贝尔恩德的建议。IrcCoaAutht是C++ 20特定的关键字,我现在被C++ 14所吸引,但是我还是会检查它。