在C ++中进行多线程处理时,“没有匹配的函数调用”错误

问题描述 投票:1回答:1

我无法理解我做错了什么。我已经经历了几个例子以及cppreference并且没有提出任何问题。

当我尝试使用for循环执行多个线程时,我调用一个函数“evaluate”。当我以串行方式运行程序时,没有编译问题,但是添加多线程会产生以下结果:

GraphEvaluate.cpp:35:70: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, const std::vector<std::vector<double> >&, const std::vector<InVec>&, InVec&, Graph&)’
   t[iii] = std::thread(evaluate, controlSpace, stateSpace, box, graph);

我不明白“评估”是如何“未解决的重载函数类型”。

这是代码:

...
std::thread t[g_threads-1];


int counter(0);


for(int iii = 0 ; iii < (g_threads - 1) ; ++iii)
{
    InVec box(stateSpace.at(counter));
    t[iii] = std::thread(evaluate, controlSpace, stateSpace, box, graph);
    counter += 1;
}


for(int iii = 0 ; iii < (g_threads - 1) ; ++iii)
{
    t[iii].join();
}
...

和评估功能:

void evaluate(const std::vector<std::vector<double>> &controlSpace, 
    const std::vector<InVec> &stateSpace, InVec box, Graph &graph)
{
    std::vector<InVec> boxList;  // create empty vector of InVec objects
    SPnode ASP(box);             // create subpaving node with box
    mince(ASP, g_epsilon);       // mince box
    treeToList(ASP, boxList);    // convert tree to list for efficient mapping


    // map each box in boxList with mapping defined in GraphMapping.cpp for each
    // controller value
    for (auto control : controlSpace)
    {
        ImList imageList;

        for (auto box : boxList)
        {
            imageList.addBox(mapping(box, control));
        }

        InVec intersectionBox(inclusionMap(imageList));
        std::vector<InVec> intersectingBoxes;  // list of boxes in state space
        // that intersect with intersectionBox

        for (auto ssBox : stateSpace)
        {
            if (!(noIntersection(ssBox, intersectionBox))) 
                intersectingBoxes.push_back(ssBox);
        }


        std::vector<int> nodeList; // list of nodes that box (function input)
        // points to with the given control

        if (!(intersectingBoxes.empty()))
        {
            for (auto ssBox : intersectingBoxes)
            {
                for (auto image : imageList.getList())
                {
                    if (!(noIntersection(ssBox, image)))
                    {
                        nodeList.push_back(ssBox.getBoxNumber());
                        break;
                    }
                }
            }
        }


        if (!(nodeList.empty()))
        {
            for (auto node : nodeList)
            {
                graph.setAdjList(box.getBoxNumber(), Edge(node, control));
            }
        }
    }
}

任何帮助表示赞赏。

c++ multithreading
1个回答
2
投票

std::thread的构造函数推导出它的参数类型并按值存储它们。

C ++模板函数参数类型推导机制从T类型的参数中推导出类型T&。因此,std::thread的所有参数都是通过值传递的。 @MaximEgorushkin

如果你需要调用一个函数,它的参数是来自std::thread的引用,请使用std::ref()包装参数。例如:

std::thread(evaluate, std::ref(controlSpace), std::ref(stateSpace), box, std::ref(graph));

另一方面,您需要更改evaluate函数的声明,如:

void evaluate(std::vector<std::vector<double>> controlSpace, 
    std::vector<InVec> stateSpace, InVec box, Graph graph);

有关更多信息,请参阅this post

© www.soinside.com 2019 - 2024. All rights reserved.