Mateus Vicente
Mateus Vicente

Reputation: 11

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.

Upvotes: 0

Views: 1190

Answers (3)

Jagannath
Jagannath

Reputation: 4025

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

Upvotes: 1

FireRain
FireRain

Reputation: 113

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.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

You don't want to pass a pointer to that thread function:

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

Upvotes: 2

Related Questions