mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 10:04:35 +03:00
19
CPP11.md
19
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.
|
||||
|
||||
Reference in New Issue
Block a user