I had hard time understanding how to use async function.
I don't want to use boost library but just want to run a function asynchronously, so I ask your help:
Here's a piece of my code:
void KWxAPI::update(bool const no_interact)
{
async(launch::async, &KWxAPI::syncUpdate, no_interact);
}
bool KWxAPI::syncUpdate(bool const nointeract)
{
//blablabla
}
And here are the errors the build gimme: See on pastebin
I really hope you'll help me cause I come from Java and C++ is much harder :C
EDIT: I'm using MSVC.
Your code doesn't compile because you're trying to invoke a pointer to a member function, without passing the object to invoke it on.
KWxAPI::syncUpdate
needs a KWxAPI
object to use as it's this
pointer, but you aren't passing one. Any time you try to invoke a pointer-to-member function, such as this, you need to pass the this
pointer it should use as the first parameter.
std::async(std::launch::async, &KWxAPI::syncUpdate, this, no_interact);
Aside from this, you should be aware that KWxAPI::syncUpdate
will execute in another thread, but your std::async
statement will block until that thread finishes executing the function.
This is because std::async
returns an std::future
object, which is destructed at the end of your std::async
statement, because you aren't storing it anywhere. In it's destructor, it blocks until execution has completed.