Based on this question: Timeout for thread.join() (ManuelAtWork's answer) I tried to implement a timeout for my std::threads:
std::vector<std::shared_ptr<TestFlow>> testFlowObjects;
std::thread workerThreads[MAX_PARALLEL_NR]; // maximum of 32 threads
std::vector<std::future<void>> helperThreads;
for(int readerCounter=0; readerCounter<GetNumberOfReader(); readerCounter++)
{
testFlowObjects.push_back(std::make_shared<TestFlow>(m_logFiles));
testFlowObjects.back()->SetThreadID(readerCounter);
testFlowObjects.back()->SetTestResults(m_testResultsVector); // vector of int
workerThreads[readerCounter] = std::thread(&TestFlow::DoWork, testFlowObjects.back());
}
// wait for all threads
for(int threadCount=0; threadCount<GetNumberOfReader(); threadCount++)
{
// use helper threads to be able to join with timeout
helperThreads.push_back(std::async(std::launch::async, &std::thread::join, &workerThreads[threadCount]));
helperThreads.back().wait_for(std::chrono::seconds(5)); // 5 sec
}
It works fine if I use a join instead of the std::future helper thread code, but I can't wait infinite!
With std::future approach it seems not all threads are finished and I got: R6010: abort() has been called
Any ideas how to do it correctly?
I think I have to change it like this:
if(helperThreads.back().wait_for(std::chrono::seconds(5)) == std::future_status::timeout) // WHAT SHOULD I DO HERE???