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?
Parallel processing can be described as a class of techniques which enables the system to achieve simultaneous data-processing tasks to increase the computational speed of a computer system. A parallel processing system can carry out simultaneous data-processing to achieve faster execution time.
Parallel programming is the process of using a set of resources to solve a problem in less time by dividing the work. Using parallel programming in C is important to increase the performance of the software. Concurrent vs Parallel: How Does Parallel Programming Differ From Multithreaded Programming? Parallel programming is a broad concept.
My function parallel_for () (define later in the post) splits a for loop into smaller chunks (sub loops), and each chunk assigned to a thread. Here is the usage:
Parallel programming is a broad concept. It can describe many types of processes running on the same machine or on different machines. Multithreading specifically refers to the concurrent execution of more than one sequential set (thread) of instructions. Multithreading is programming multiple, concurrent execution threads.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With