Last blog< C++11 Concurrent Guide I (A Preliminary Study of C++11 Multithreading) > It just mentions the basic usage of std::thread and gives a simple example. This article will introduce the usage of std::thread in some detail.
std::thread is declared in the < thread > header file, so the # include < thread > header file is required when using std::thread.
std::thread construction
- (1). The default constructor creates an empty thread execution object.
- (2) Initialize the constructor and create a thread object, which can be joinable. The newly generated thread will call the fn function, and the parameters of the function are given by args.
- (3) The copy constructor (disabled) means that thread s cannot be copied.
- (4). The move constructor, the move constructor, does not represent any thread ed e x ecution object after a successful call.
- Note: threads that can be joinable must be joined by the main thread or set to detached before they are destroyed.
Examples of std::thread constructors link
#include <iostream> #include <utility> #include <thread> #include <chrono> #include <functional> #include <atomic> void f1(int n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread " << n << " executing\n"; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void f2(int& n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int main() { int n = 0; std::thread t1; // t1 is not a thread std::thread t2(f1, n + 1); // pass by value std::thread t3(f2, std::ref(n)); // pass by reference std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread t2.join(); t4.join(); std::cout << "Final value of n is " << n << '\n'; }
move assignment operation
- (1) The motion assignment operation, if the current object is not joinable, needs to pass a right-value reference (rhs) to the motion assignment operation; if the current object can be joinable, terminate() will report an error.
- (2) Copy assignment is disabled and thread s cannot be copied.
See the following examples:
#include <stdio.h> #include <stdlib.h> #include <chrono> // std::chrono::seconds #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::sleep_for void thread_task(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); std::cout << "hello thread " << std::this_thread::get_id() << " paused " << n << " seconds" << std::endl; } /* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */ int main(int argc, const char *argv[]) { std::thread threads[5]; std::cout << "Spawning 5 threads...\n"; for (int i = 0; i < 5; i++) { threads[i] = std::thread(thread_task, i + 1); } std::cout << "Done spawning threads! Now wait for them to join\n"; for (auto& t: threads) { t.join(); } std::cout << "All threads joined.\n"; return EXIT_SUCCESS; }
Other member functions
- get_id Get the thread ID.
- joinable Check whether threads can be join ed.
- join Join thread.
- detach Detach threads
- swap Swap threads.
- native_handle Return native handle.
- hardware_concurrency [static] Detect hardware concurrency features.