Start 4 threads asynchronously, wait for all of them to finish
for (int i = 0; i < 4; ++i)
{
future<KaldiWrapSegment> future = std::async(std::launch::async,
Decode, param1, param2, param3);
std::this_thread::sleep_for(2s);
threadReturnFutures.push_back(std::move(future));
log("Started async decoding");
}
// wait for threads to finish
Res res;
for (int i = 0; i < threadReturnFutures.size(); ++i)
{
res = threadReturnFutures[i].get();
log("Async decoding #" + std::to_string(i) + " is now finished");
}
{
Message msg;
bool received = messageBuffer.waitForResponse(msgId, msg, timeoutWaitingForResponseMessageSeconds);
if (received && msg.msgType == CorrectValue)
{
pid = msg.fromPid;
return true;
}
return false;
};
if (!RepeatUntilTrueOrTimeout(waitingForIt, timeoutWaitingForResponseMessageSeconds))
{
problem
}
bool RepeatUntilTrueOrTimeout(std::function<bool()> fn, int timeoutSeconds)
{
auto actionTimeoutEnd = std::chrono::system_clock::now() + std::chrono::seconds(timeoutSeconds);
while (true)
{
if (fn())
{
return true;
}
auto now = std::chrono::system_clock::now();
if (now > actionTimeoutEnd)
{
return false;
}
}
}