Add Threads library (#37)

* Add Threads library
This commit is contained in:
Tulio Leao
2018-08-12 15:34:30 -03:00
committed by Anthony Calandra
parent 79c439134f
commit 36729c1716
2 changed files with 36 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ C++11 includes the following new language features:
C++11 includes the following new library features: C++11 includes the following new library features:
- [std::move](#stdmove) - [std::move](#stdmove)
- [std::forward](#stdforward) - [std::forward](#stdforward)
- [std::thread](#stdthread)
- [std::to_string](#stdto_string) - [std::to_string](#stdto_string)
- [type traits](#type-traits) - [type traits](#type-traits)
- [smart pointers](#smart-pointers) - [smart pointers](#smart-pointers)
@@ -594,6 +595,21 @@ wrapper(a); // copied
wrapper(std::move(a)); // moved wrapper(std::move(a)); // moved
``` ```
### std::thread
The `std::thread` library provides a standard way to control threads, such as spawning and killing them. In the example below, multiple threads are spawned to do different calculations and then the program waits for all of them to finish.
```c++
void foo(bool clause) { /* do something... */ }
std::vector<std::thread> threadsVector;
threadsVector.emplace_back([]() {
// Lambda function that will be invoked
});
threadsVector.emplace_back(foo, true); // thread will run foo(true)
for (auto& thread : threadsVector)
thread.join(); // Wait for threads to finish
```
### std::to_string ### std::to_string
Converts a numeric argument to a `std::string`. Converts a numeric argument to a `std::string`.
```c++ ```c++
@@ -718,6 +734,8 @@ See the section on [smart pointers](#smart-pointers) for more information on `st
### Memory model ### Memory model
C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables. C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables.
See the sections on: [std::thread](#stdthread)
## Acknowledgements ## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [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. * [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

@@ -74,6 +74,7 @@ C++11 includes the following new language features:
C++11 includes the following new library features: C++11 includes the following new library features:
- [std::move](#stdmove) - [std::move](#stdmove)
- [std::forward](#stdforward) - [std::forward](#stdforward)
- [std::thread](#stdthread)
- [std::to_string](#stdto_string) - [std::to_string](#stdto_string)
- [type traits](#type-traits) - [type traits](#type-traits)
- [smart pointers](#smart-pointers) - [smart pointers](#smart-pointers)
@@ -1143,6 +1144,21 @@ wrapper(a); // copied
wrapper(std::move(a)); // moved wrapper(std::move(a)); // moved
``` ```
### std::thread
The `std::thread` library provides a standard way to control threads, such as spawning and killing them. In the example below, multiple threads are spawned to do different calculations and then the program waits for all of them to finish.
```c++
void foo(bool clause) { /* do something... */ }
std::vector<std::thread> threadsVector;
threadsVector.emplace_back([]() {
// Lambda function that will be invoked
});
threadsVector.emplace_back(foo, true); // thread will run foo(true)
for (auto& thread : threadsVector)
thread.join(); // Wait for threads to finish
```
### std::to_string ### std::to_string
Converts a numeric argument to a `std::string`. Converts a numeric argument to a `std::string`.
```c++ ```c++
@@ -1267,6 +1283,8 @@ See the section on [smart pointers](#smart-pointers) for more information on `st
### Memory model ### Memory model
C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables. C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables.
See the sections on: [std::thread](#stdthread)
## Acknowledgements ## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [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. * [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.