Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ futures parallel processing

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?

like image 206
Andy Walton Avatar asked Aug 11 '15 12:08

Andy Walton


People also ask

What is parallel processing?

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.

What is parallel programming in C?

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.

What does the function parallel_for () do?

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:

What is the difference between parallel programming and multithreading?

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.


1 Answers

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.

like image 113
user1531083 Avatar answered Oct 24 '22 23:10

user1531083