Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 965
user0000001 Avatar asked Mar 12 '23 23:03

user0000001


1 Answers

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

like image 134
Ajay Avatar answered Mar 14 '23 11:03

Ajay