C++11 feature: std::async (#47)

* C++11 feature: std::async
This commit is contained in:
defmys
2018-12-11 00:33:12 +08:00
committed by Anthony Calandra
parent 9f37fd1773
commit 4df2261e0c
2 changed files with 38 additions and 0 deletions

View File

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

View File

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