diff --git a/CPP11.md b/CPP11.md index b31dcac..b08b612 100644 --- a/CPP11.md +++ b/CPP11.md @@ -45,6 +45,7 @@ C++11 includes the following new library features: - [unordered containers](#unordered-containers) - [std::make_shared](#stdmake_shared) - [memory model](#memory-model) +- [std::async](#stdasync) ## C++11 Language Features @@ -738,6 +739,24 @@ C++11 introduces a memory model for C++, which means library support for threadi See the sections on: [std::thread](#stdthread) +### std::async +`std::async` runs the given function either asynchronously or lazily-evaluated, then returns a `std::future` which holds the result of that function call. + +The first parameter is the policy which can be: +1. `std::launch::async | std::launch::deferred` It is up to the implementation whether to perform asynchronous execution or lazy evaluation. +1. `std::launch::async` Run the callable object on a new thread. +1. `std::launch::deferred` Perform lazy evaluation on the current thread. + +``` +int foo() { + /* Do something here, then return the result. */ + return 1000; +} + +auto handle = std::async(std::launch::async, foo); // create an async task +auto result = handle.get(); // wait for the result +``` + ## Acknowledgements * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. diff --git a/README.md b/README.md index 549a279..2fdaa66 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ C++11 includes the following new library features: - [unordered containers](#unordered-containers) - [std::make_shared](#stdmake_shared) - [memory model](#memory-model) +- [std::async](#stdasync) ## C++17 Language Features @@ -1330,6 +1331,24 @@ C++11 introduces a memory model for C++, which means library support for threadi See the sections on: [std::thread](#stdthread) +### std::async +`std::async` runs the given function either asynchronously or lazily-evaluated, then returns a `std::future` which holds the result of that function call. + +The first parameter is the policy which can be: +1. `std::launch::async | std::launch::deferred` It is up to the implementation whether to perform asynchronous execution or lazy evaluation. +1. `std::launch::async` Run the callable object on a new thread. +1. `std::launch::deferred` Perform lazy evaluation on the current thread. + +``` +int foo() { + /* Do something here, then return the result. */ + return 1000; +} + +auto handle = std::async(std::launch::async, foo); // create an async task +auto result = handle.get(); // wait for the result +``` + ## Acknowledgements * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.