2017-06-21 5 views
1

ausführen Die Frage, die ich habe, ist die Zeile, wo ich Fehler angeben gibt Fehler als std :: thread -Konstruktor arbeitet die Funktion aufgerufen werden und benötigt den Parameter wie von der Funktionssignatur benötigt.Fähigkeit, eine paketierte Aufgabe mit std :: bind Funktionsparameter in einem separaten Thread über Template-Funktion

Gibt es eine Möglichkeit, dies zu lösen? Wenn ich versuche, den Funktionsnamen und die Argumentliste von paketed_task zu entschlüsseln, dann kann ich die get_future-Funktion nicht über die paketierte Aufgabe verwenden und muss meinen eigenen Versprechens-/zukünftigen Code hinzufügen, um dies zu handhaben.

#include<iostream> 
#include<string> 
#include<thread> 
#include<future> 
#include<functional> 

using namespace std; 

int sampleAddFunction(const int& a, const int& b) 
{ 
    int sum = a + b; 
    cout << "sum = " << sum << endl; 
    return(sum); 
} 

template<typename T> T asyncExecutor(std::packaged_task<T(T, T)>&& package) 
{ 
    std::future<T> result = package.get_future(); 
    std::thread task_td(std::move(package)); // ERROR here as the std::thread identifies the function name from package and requires the params to be passed. How to handle this ? 
    task_td.join(); 
    return(result.get()); 
} 

int main(int argc, char* argv[]) 
{ 

    // Executing via directly calling through main. 
    int testResult1 = sampleAddFunction(100, 200); 
    cout << "testResult1 = " << testResult1 << endl; 

    // Attempt to create a std::packaged_task and then run it in another thread. 
    std::packaged_task<int(int,int)> task(std::bind(sampleAddFunction, 10, 20)); 
    std::future<int> result = task.get_future(); 
    std::thread t(std::move(task), 100, 200); // 100 and 200 are dummy parameters. 
    t.join(); 
    int testResult2=result.get(); 
    cout << "testResult2 = " << testResult2 << endl; 

    // Attempt to run this in seperate thread and get results. 
    std::packaged_task<int(int,int)> task2(std::bind(sampleAddFunction, 15, 27)); 
    int testResult3 = asyncExecutor<int>(std::move(task2), 100, 200); 
    cout << "testResult3 = " << testResult3 << endl; 

} 

Antwort

0

Dies sollte funktionieren.

#include<iostream> 
#include<string> 
#include<thread> 
#include<future> 
#include<functional> 

using namespace std; 

int sampleAddFunction(int a, int b) 
{ 
    int sum = a + b; 
    cout << "sum = " << sum << endl; 
    return(sum); 
} 

template<typename R, typename F, typename... Ts> 
R asyncExecutor(F&& package, Ts... args) 
{ 
    std::future<R> result = package.get_future(); 
    std::thread task_td(std::move(package), args...); 
    task_td.join(); 
    return(result.get()); 
} 

int main(int argc, char* argv[]) 
{ 
    std::packaged_task<int(int,int)> task2(sampleAddFunction); 
    int testResult3 = asyncExecutor<int>(std::move(task2), 15, 27); 
    cout << "testResult3 = " << testResult3 << endl; 
} 
0

Sie sind ein binäres packaged_task (std::packaged_task<int(int,int)>) von einer nullary Funktion, das Ergebnis Ihrer bind Konstruktion (std::function<int()>).

Sie sollten entweder nicht verwenden binden, oder haben asyncExecutor eine nullary packaged_task akzeptieren (std::packaged_task<T()>)

int sampleAddFunction(const int& a, const int& b) 
{ 
    int sum = a + b; 
    cout << "sum = " << sum << endl; 
    return(sum); 
} 

template<typename T, typename ... ARGS> T asyncExecutor(std::packaged_task<T(ARGS...)>&& package, ARGS ... args) 
{ 
    std::future<T> result = package.get_future(); 
    std::thread task_td(std::move(package), args...); 
    task_td.join(); 
    return(result.get()); 
} 

int main(int argc, char* argv[]) 
{ 
    std::packaged_task<int()> task(std::bind(sampleAddFunction, 10, 20)); 
    int testResult = asyncExecutor(std::move(task)); 
    cout << "testResult = " << testResult << endl; 
    std::packaged_task<int(int,int)> task2(sampleAddFunction); 
    int testResult2 = asyncExecutor(std::move(task2), 15, 27); 
    cout << "testResult2 = " << testResult2 << endl; 
} 
Verwandte Themen