2016-06-23 14 views
1

Ich verstehe nicht, was ich falsch gemacht habe. Ich habe mehrere Beispiele und die Referenz durchgesehen und mir nichts einfallen lassen."kein passender Funktionsaufruf" Fehler beim Multithreading in C++

Wenn ich versuche, eine Anzahl von Threads mit einer for-Schleife auszuführen, rufe ich eine Funktion "evaluate" auf. Wenn ich das Programm in Serie laufe kein Kompilieren Problem ist jedoch Multithreading Zugabe ergibt folgend:

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); 

Ich verstehe nicht, wie „bewertet“ ist ein ‚ungelöster überladener Funktionstyp‘. Hier

ist der Code:

... 
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(); 
} 
... 

Und die Funktion bewerten:

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)); 
      } 
     } 
    } 
} 

Jede Hilfe sehr geschätzt wird.

+2

Sie brauchen 'std :: ref', dh' std :: thread (auswerten, std :: ref (controlSpace), std :: ref (stateSpace), box, std :: ref (graph)); ' – chema989

Antwort

2

Der Konstruktor std::thread leitet seine Argumenttypen ab und speichert sie nach Wert.

C++ Template-Funktion Argumenttyp Abzugsmechanismus deduziert T&T aus einem Argument des Typs Typ. Daher werden alle Argumente an std::thread als Wert übergeben. @MaximEgorushkin

Wenn Sie eine Funktion aufrufen, dass seine Argumente sind Referenzen von einem std::thread, wickeln Sie das Argument std::ref() verwenden. Zum Beispiel:

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

Auf der anderen Seite müssen Sie die Deklaration von evaluate Funktion ändern wie:

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

Weitere Informationen finden Sie this post.

+0

Seltsamerweise scheint dies nicht zu funktionieren, ich bekomme die gleiche Fehlermeldung wie oben, nur mit std :: reference_wrapper vor den umgebrochenen Parametern – duncster94

+1

ist 'evaluate' eine Member-Klasse? – chema989

+0

Es stellte sich heraus, dass "evaluate" eine Funktion war, die ich in einem anderen Code verwendet hatte, aber einige meiner Objekte als Freund hinzugefügt hatte. Ich finde es immer noch seltsam, dass 'std :: thread' diese Friend-Deklaration findet, wenn keine meiner anderen Funktionen dies tut. Irgendeine Idee? – duncster94

Verwandte Themen