Why am I getting a "read access violation" exception thrown while accessing value from std::future?

467 views Asked by At

Edit: my question is different from the suggested question becaude I cannot poll 100's of future.get() and wait to execute the remaining program in main() thread. I am hoping the threads to return the value when the thread is done executing and then the calling function should run.

As per this question

in C++ future waits for the results and halts the next line of code

I am having the following code where I am creating about 100 async threads and then retrieving the return variable;

void BEVTest::runTests()
{   
    CameraToBEV cbevObj(RoiBbox, OutputDim, InputDim);
    // for multithreading
    std::vector<std::future<cv::Mat>> processingThread;
    std::vector<cv::Mat> opMatArr;
    
        for (int i = 0; i < jsonObjects.size(); i++) {
                processingThread.emplace_back(std::async(std::launch::async, &CameraToBEV::process,&cbevObj, std::ref(jsonObjects[i]), RoiBbox, OutputDim, InputDim));
        }
        for (auto& future : processingThread) {
            opMatArr.emplace_back(future.get());
        }
}

I am getting a run time exception of "read access violation" at the line opMatArr.emplace_back(future.get());

when I checked the processingThread variable, it shows all the future values as pending. So, if the above quote from the question is correct, shouldn't my code wait till it gets all the future values? Else otherwise this answer provides the following solution to wait till you get the future value from future.get()

auto f = std::async(std::launch::async, my_func)
    .then([] (auto fut) {
        auto result = fut.get();
        /* Do stuff when result is ready. */
    });

But this won't be possible for me because then I will have to poll all 100 future.get() and it will have an overhead which will defeat the purpose of creating threads.

I have two questions; 1> Why am I getting a run time exception? 2> How do I wait till all 100's of future.get() return value?

EDIT: I am providing my process function.

cv::Mat CameraToBEV::process(json& jsonObject, std::vector<Point2f> roibbox, Point2f opDim, Point2f ipDim)
{   // Parameters
    img_width = imgWidth = opDim.x;
    img_height = imgHeight = opDim.y;
    for (int i=0; i<roibbox.size(); i++)
    {
        roiPoints[i] = roibbox[i];
    }

    // From Centroids class
    jsonObj = jsonObject; 
    initializeCentroidMatrix();
    getBBoxes();
    transformCoordinates();

    // From Plotter class
    cv::Mat resultImg = plotLocation(finalCentroids,violationArr, ipDim, opDim);
    return resultImg;
}
0

There are 0 answers