简体   繁体   English

线程传递字符串参数的布尔函数

[英]Threading a bool function passing string argument

I am new working with threads.. but I got the concept and have been playing with it in the last days. 我是线程的新手..但是我得到了这个概念,并且在最后几天一直在使用它。 But now I am trying to create a thread calling a bool function and passing a string as argument. 但是现在我试图创建一个调用bool函数并传递字符串作为参数的线程。 The code is basically: 代码基本上是:

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

and then I call equalise from other place. 然后我从其他地方叫均衡。

But when I try to compile it I get the following error: 但是当我尝试编译它时,出现以下错误:

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

Any idea about how I can fix that? 关于我该如何解决的任何想法?

Many thanks for the help. 非常感谢您的帮助。

You don't want to pass a pointer to that thread function: 您不想将指针传递给该线程函数:

  std::thread analyse_dat0(analyseData, filename0); // omit the &
                                                    // address of operator

Instead of using std::thread for this purpose why not use std::async and get the result as std::future ? 为什么不使用std::async并将结果作为std::future来使用,而不是使用std::thread That's much simpler, IMO. IMO,这要简单得多。

Class c; 
auto ft = std::async([&] { return c.analyseData("file.txt"); });    
bool result = ft.get();

Remove the & from td::thread analyse_dat0(analyseData, &filename0); td::thread analyse_dat0(analyseData, &filename0);删除& td::thread analyse_dat0(analyseData, &filename0); as you want a reference not a pointer in analyseData. 因为您想要引用而不是analyseData中的指针。
Also you need to provide an object as analyseData isn't static. 另外,您需要提供一个对象,因为analyseData不是静态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM