如何在从另一个 class 的函数调用 std::async 时传递另一个 class 的函数?

How do I pass a function from another class while calling std::async from a function of different class?

我有两个 class,CameraToBEVBEVTestCameraToBEV 包含函数 process().

class CameraToBEV : 
{
    public:
        CameraToBEV(std::vector<Point2f>, cv::Point2f, cv::Point2f);
        /*
            process(json Object, ROI Bounding box, Output Dimensions, Input Dimensions)
        */
        cv::Mat process(json&, std::vector<Point2f> = { Point2f(765,57), Point2f(1860,57), Point2f(27, 1000) ,Point2f(1800, 1000) }, Point2f=Point2f(1920,1080), Point2f=Point2f(1920, 1080));
};

并且 class BEVTest 包含函数 runtests(),它在 for 循环中调用函数 process()

void BEVTest::runTests()
{   
    CameraToBEV cbevObj(RoiBbox, OutputDim, InputDim);

    std::vector<std::future<cv::Mat>> processingThread;
    
        for (int i = 0; i < jsonObjects.size(); i++) {

            processingThread.emplace_back(std::async(std::launch::async, &CameraToBEV::process,this, jsonObjects[i]));
            
                
        }
    
}
 

我正在尝试使用 for loopstd::async 进行多线程处理,以便为每个 JSON 对象并行执行处理函数。

但是当我构建此代码时出现以下错误;

Error   C2672   'std::async': no matching overloaded function found 
 
Error   C2893   Failed to specialize function template 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)' 

所以我尝试了另一种调用方式 std::async

processingThread.emplace_back(std::async(std::launch::async, &cbevObj.process,this, jsonObjects[i]));

processingThread.emplace_back(std::async(std::launch::async, &cbevObj.process,jsonObjects[i]));

processingThread.emplace_back(std::async(std::launch::async, &cbevObj.process,jsonObjects[i], ROIBbox, OutputDim, InputDim));

我仍然遇到错误。

注:

I am able to call std::async without any error or problem if I call the function of the same class from another function of the same class which has a for loop, as It's only when I am calling a function of a different class from another class that has for loop that I am facing errors.

编辑: 尝试评论后

processingThread.emplace_back(std::async(std::launch::async, &CameraToBEV::process,&cbevObj, jsonObjects[i], RoiBbox, OutputDim, InputDim));

或者不将默认值传递给 process()

processingThread.emplace_back(std::async(std::launch::async, &CameraToBEV::process,&cbevObj, jsonObjects[i]));

错误是:

Error   C2672   'std::async': no matching overloaded function found 

Error   C2893   Failed to specialize function template 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)'   

Error   C2672 std::vector<std::future<cv::Mat>,std::allocator<std::future<cv::Mat>>>::emplace_back': no matching overloaded function found  

BEVTests 中的 jsonObjects 声明:

class BEVTest
{
    Point2f InputDim, OutputDim;
    std::vector<Point2f> RoiBbox;
    std::string outputPath, inputJsonPath;
    std::vector<json> jsonObjects;

public:  
    BEVTest(std::string, std::string, std::vector<cv::Point2f>, cv::Point2f, cv::Point2f);
    void loadData(); 
    void runTests();
    json parseJson(std::string);
};

根据评论,如果您要使用 std::async 运行 的成员函数是 CameraToBEV::process,那么在 &CameraToBEV::process 之后立即传递的参数应该是指向有效 CameraToBEV 个实例。

此外,std::async默认情况下将按值传递参数。但是您调用的函数具有签名...

cv::Mat process(json&, std::vector<Point2f> = { Point2f(765,57), Point2f(1860,57), Point2f(27, 1000) ,Point2f(1800, 1000) }, Point2f=Point2f(1920,1080), Point2f=Point2f(1920, 1080));

所以它期望第一个参数是对 json 对象的非常量引用。因此,对 std::async 的调用应该是(未经测试的)...

processingThread.emplace_back(std::async(std::launch::async, &CameraToBEV::process, &cbevObj, std::ref(jsonObjects[i])));

(注意std::ref的用法)