c++ - 如何从另一个类的函数调用std::async时传递另一个类的函数?

标签 c++ multithreading stdasync

我有两个类,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));
};
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]));
            
                
        }
    
}
 
我正在尝试使用std::asyncfor loop进行多线程处理,以便对每个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 mentioned in this answer. 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的使用)

关于c++ - 如何从另一个类的函数调用std::async时传递另一个类的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65842878/

相关文章:

c++ - 无法从类中访问全局函数

java - 同步发生了一些奇怪的事情(Test2.class)

c - 如何在不使用 POSIX 库 <pthread.h> 的情况下在 C 中创建线程

c++ - 我可以在不等待 future 限制的情况下使用 std::async 吗?

c++ - 为什么切片会准确发生?

c++ - 在 C++ 中动态创建函数调用

c++ - 从 std::async 返回的 std::future 在超出范围时挂起

c++ - std::async 返回值。未知错误

c++ - 使用 C++ 中的最大似然法将 1000 个样本拟合为正态分布

python - 以多线程方式加载多个 npz 文件