Reputation: 249
I'm using std::futures
to parallel process my algorithm. I split up the information into mutually exclusive pools and then perform the same operation on each pool in its own thread. The code looks like this:
class Processor
{
public:
Processor(const std::string &strVal) : m_strVal(strVal)
{
}
std::string GetVal() const {return m_strVal;}
std::vector<std::string> Do()
{
// do some processing - this can throw an exception
}
private:
std::string m_strVal;
};
class ParallelAlgo
{
private:
std::vector<std::string> m_vecMasterResults;
public:
ProcessingFunction(const std::vector<std::string> &vecInfo)
{
// vecInfo holds mutually exclusive pools
std::vector<std::future<std::vector<std::string> > > vecFutures(vecInfo.size());
try
{
for (auto n = 0 ; n < vecInfo.size() ; n++)
{
vecFuture[n] = std::async(std::launch::async, &ParallelAlgo::WorkFunc, vecInfo[n].GetVal());
}
for (auto it = vecFutures.begin() ; it != vecFutures.end() ; ++it)
{
std::vector<std::string> RetVal = it->get();
m_MasterResults.insert(m_MasterResults.begin(), RetVal.begin(), RetVal.end());
vecFutures.erase(it);
}
}
catch (exception &e)
{
for (auto it = vecFutures.begin() ; it != vecFuture.end() ; ++it)
{
// race condition?
if (it->valid())
{
it->wait_for(std::chrono::second(0));
}
}
}
}
std::vector<std::string> ParallelAlgo::WorkFunc(const std::string &strVal)
{
Processor _Proccessor(strVal);
return _Processor.Do();
}
};
My question is how to handle the situation when an exception is thrown in Processor:Do()
? Currently I catch the exception using a future
, and then wait zero seconds for each future
that hasn't finished; this is fine - these threads will simply terminate and the processing will not be completed. However, have I not introduced a race condition in the catch block. a future
could finish between the calls to valid()
and wait_for()
, or is this not a concern as I'm not calling get()
on these incomplete futures?
Upvotes: 13
Views: 1437
Reputation: 780
A call to valid
only checks if there is a corresponding shared state, which will still be true for a finished thread until you call get
on it.
There is no race condition there.
Upvotes: 3