How to move an object to std :: async ()?

I need to move the object to an asynchronous function so that another function can manage my resources. But it seems very complicated. For example, I want to send fstream for an asynchronous function.

void asv(std::ofstream s)
{
//do something.
}

I want:

std::ofstream s("afs");
std::async(asv,std::move(s));

It cannot be compiled.

But

std::ofstream s("afs");
asv(std::move(s));

can be compiled.

How can i do this?

+4
source share
1 answer

This is the right way to do this. (Literally nothing to add)

If you are testing it with something like Coliru, note that you will not see any output, say

void asv(std::string s){
    std::cout << s << std::endl;
}

(std::launch::async std::launch::deferred) std::launch::deferred, . ( .)


, , . , std::thread - , .

--- ---
, libstd++ swap move ( 27.9). , GCC Clang libstd++, , , , .

, Visual Studio , , , . . . , , std::async .

+5

Source: https://habr.com/ru/post/1526015/


All Articles