user0000001
user0000001

Reputation: 2233

Trying to get async to work in a Windows C++ application

I can't seem to understand why this won't work. I've searched endlessly and don't see how my example below doesn't kick off an asynchronous operation.

void Folder::NewFileAction()
{
     if (Folder::Match) 
     {
         LOG(LOG_INFO) << "New file detected. Compressing";
         auto Compress = async(launch::async, &ZipFile, Folder::FilePath);
     }
}

Shouldn't this kick off an asynchronous operation in another thread? Is there a flag I have to enable in Visual Studio 2015?

Thank you

Upvotes: 4

Views: 458

Answers (1)

Ajay
Ajay

Reputation: 18441

std::async returns a std::future object. Since Compress is local object (of type std::future), and will go out of scope. Since this is only object holding the async return result, the destructor will keep on waiting. You should keep such object(s) in member of this class (a vector<future> may be).

Upvotes: 7

Related Questions