You need to include #include <thread> header files when using Std::thread, define classes that represent threads, classes and methods for mutually exclusive access, and so on.
Reference URL:
Member types and member functions:
Std::thread mainly declares three kinds of functions: (1), constructor, copy constructor (copy constructor is disabled, meaning thread cannot be copied, but can be transferred (move) or swap) and destructor, (2), member function, (3), Static member function (Hardware_concurrency), which detects hardware concurrency characteristics.
The constructor functions are as follows:
Some of the relevant data structures and storage locations:
On the stack thread T1 (show); Executes the thread T2 (show) based on the function initialization; thread t3 (show); Threading Array Thread Th[3]{thread (show), Thread (show), Thread (show)}; Heap on thread *pt1 (new Thread (show)); Thread *pt2 (new Thread (show)); Thread *pt3 (new Thread (show)); Array of thread pointers
Thread initialization (this implements the Multithreading pass parameter)
void Show (const char *STR, const int ID); int main () { thread T1 (show, "hello!", 0); The three parameters are the function name, and its two parameters, thread T2 (show, "c++! ", 1); return 0; }
Join: Calling this function will block the current thread. Blocks the thread where the caller (caller) is located until the thread that is identified by the Std::thread object of the join executes the end;
Detach: Separates the execution instance represented by the current thread object from the thread object so that the execution of the thread can be performed separately. Once the thread has finished executing, the resources it allocates will be freed.
- At any point in time, the thread is either associative (joinable), or detached (detached). A binding thread can be recovered by another thread and killed, and its memory resource (such as a stack) is not freed until it is reclaimed by another thread. Instead, a disconnected thread cannot be recycled or killed by other threads, and its memory resources are automatically freed by the system when it terminates.
- Threads.joinable () determines whether a thread can join; Threads.join (); The main thread waits for the current threads to finish executing before exiting;
Th.detach ();
//out of the main thread of the binding, the main thread is hung, the child thread does not error, the child thread after the execution of automatic exit. after//detach, the child threads become orphaned threads and cannot communicate between threads.
---------------------------------
Get the number of CPU cores:
n = thread::hardware_concurrency (); //
Atomic variables and thread safety: there is a conflict between threads (there may be num++ overlap in the following code)
#include <iostream> #include <thread> #include <mutex> using namespace std; const int N = 100000000; int num (0); Mutex m; void Run () {for (int i = 0; i < N; i++) { m.lock (); num++; M.unlock (); } } int main () { clock_t start = Clock (); Thread T1 (run); Thread T2 (run); T1.join (); T2.join (); clock_t end = Clock (); cout << "num=" << num << ", spents" << end-start << "MS" << Endl; return 0; }
There is a problem: The calculation is slow, the main reason is that the mutex plus unlocking takes time. Std::mutex.
#include <iostream> #include <thread> #include <atomic> using namespace std; const int N = 100000000; atomic_int num{0};//No thread conflicts, thread-safe void run () {for (int i = 0; i < N; i++) { num++; } } int main () { clock_t start = Clock (); Thread T1 (run); Thread T2 (run); T1.join (); T2.join (); clock_t end = Clock (); cout << "num=" << num << ", spents" << end-start << "MS" << Endl; return 0; }
After the atomic variable, the result is correct and the calculation speed is general. Reference std::atomic
But in fact, you can increase the speed of calculations by using join.
Thread T1 (run); T1.join (); End before returning to the main thread thread T2 (run);
------------------------
The question of time waiting
#include <iostream> #include <thread> #include <chrono> using namespace std; int main () { thread Th1 ([] () { //Let thread wait 3 seconds this_thread::sleep_for (Chrono::seconds (3)); Let the CPU perform other idle threads This_thread::yield (); Thread ID cout << this_thread::get_id () << Endl; }); return 0; }
- The yield () function can be used to jump the caller thread out of the running state and be handed over to the operating system for scheduling, that is, the current thread discards execution, and the operating system schedules another thread to continue execution;
- The Sleep_until () function is a thread that sleeps to a specified moment (time point), and the thread is awakened again;
- The Sleep_for () function is the thread that sleeps a specified time span, and the thread is woken up, but actual sleep may actually be longer than the time slice represented by sleep_duration due to thread scheduling.
-------------------------------
Swapping of threads using swap (t1, T2);
Thread movement using thread t2 = move (t1);
Multithreading in C + +