简体   繁体   中英

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

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 ? That's much simpler, 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); as you want a reference not a pointer in analyseData.
Also you need to provide an object as analyseData isn't static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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