dimanche 28 octobre 2018

Starting a thread or future with an std::string () const function

I would like to spawn a thread or a future calling this function:

std::string myClass::myFunction() const noexcept

I first tried spawning them both like this:

thread t0(myFunction); auto f1 = async(myFunction);

I would then get this error:

no instance of constructor "std::thread::thread" matches the argument list. argument types are: (std::string () const)

I then read online that since it is const I would need to call it by reference but all methods online have not worked, I am unsure if the problem is because the function is const or not. What I have tried:

thread t0(&myClass::myFunction); auto f1 = async(&myClass::myFunction);

and

thread t0(&myFunction); auto f1 = async(&myFunction);

After this I would have two new errors that I cannot find a solution to:

std::invoke: no matching overloaded function found.

and

Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

I think this is due to my lack of understand on how threads and futures work but I am struggling to find literature that gives me any relation to this. Basically I don't really understand what rule I am breaking, if there is a fundamental flaw in my approach any direction or tips would be nice. Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire