diff --git a/CPP11.md b/CPP11.md index 1e8df39..9ed23ff 100644 --- a/CPP11.md +++ b/CPP11.md @@ -34,6 +34,7 @@ C++11 includes the following new language features: C++11 includes the following new library features: - [std::move](#stdmove) - [std::forward](#stdforward) +- [std::thread](#stdthread) - [std::to_string](#stdto_string) - [type traits](#type-traits) - [smart pointers](#smart-pointers) @@ -594,6 +595,21 @@ wrapper(a); // copied 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 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 Converts a numeric argument to a `std::string`. ```c++ @@ -718,6 +734,8 @@ See the section on [smart pointers](#smart-pointers) for more information on `st ### 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. +See the sections on: [std::thread](#stdthread) + ## 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 7b04b5b..dffc0ad 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ C++11 includes the following new language features: C++11 includes the following new library features: - [std::move](#stdmove) - [std::forward](#stdforward) +- [std::thread](#stdthread) - [std::to_string](#stdto_string) - [type traits](#type-traits) - [smart pointers](#smart-pointers) @@ -1143,6 +1144,21 @@ wrapper(a); // copied 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 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 Converts a numeric argument to a `std::string`. ```c++ @@ -1267,6 +1283,8 @@ See the section on [smart pointers](#smart-pointers) for more information on `st ### 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. +See the sections on: [std::thread](#stdthread) + ## 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.