[英]Threading a bool function passing string argument
我是線程的新手..但是我得到了這個概念,並且在最后幾天一直在使用它。 但是現在我試圖創建一個調用bool函數並傳遞字符串作為參數的線程。 代碼基本上是:
bool className::analyseData(const std::string& filename) {
...
return true;
}
bool className::equalise(...) {
...
const std::string filename0 = filenameBase + "_chip" + ss.str() + "_0";
std::thread analyse_dat0(analyseData, &filename0);
...
return true;
}
然后我從其他地方叫均衡。
但是當我嘗試編譯它時,出現以下錯誤:
SpidrEqualisation_multi_threading.cpp:140:50: error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, const string&)
std :: thread analyse_dat0(analyseData,filename0);`
關於我該如何解決的任何想法?
非常感謝您的幫助。
您不想將指針傳遞給該線程函數:
std::thread analyse_dat0(analyseData, filename0); // omit the &
// address of operator
為什么不使用std::async
並將結果作為std::future
來使用,而不是使用std::thread
? IMO,這要簡單得多。
Class c;
auto ft = std::async([&] { return c.analyseData("file.txt"); });
bool result = ft.get();
從td::thread analyse_dat0(analyseData, &filename0);
刪除&
td::thread analyse_dat0(analyseData, &filename0);
因為您想要引用而不是analyseData中的指針。
另外,您需要提供一個對象,因為analyseData不是靜態的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.