The identifier of the thread is the thread ID, the ID type of the thread is Std::thread::id, and can be obtained in two ways. The first is the thread object, which can be associated with threads, that calls object.get_id (), or if object does not have any threads associated with it, then get_id () returns the Std::thread::id default constructor, which is no thread. Another method is to call std::this_thread::get_id () in the currently running thread.
The ID of the thread can be copied and compared. If two thread IDs are equal, then they are the same thread, or none of them are associated to the thread. If the two IDs are different, then they are different threads, or they are one associated to the thread and the other is not associated to the thread.
Std::thread::id provides all the comparison operators. You can use associative containers, sorting, or comparisons.
The thread ID is often used to determine if the thread needs to perform certain operations. For example, the initialization thread creates other threads that are used to perform different parts of the algorithm. The other threads are then logged before the other thread is created, and then other threads are created. You can then determine whether the current thread is an initialization thread.
Std::thread::id master_thread;//Record main thread (initialization thread) void Some_core_part_of_algorithm () {if (std::this_thread::get_id () = = Master_thread) {//Initialize the thread's work do_master_thread_work ();} Do_common_work ();}
The thread ID can be used as the key for the associated container. For example, a container can be used to control the thread to store information and exchange information between threads.
You can use stream output to output the thread ID
STD::COUT<<STD::THIS_THREAD::GET_ID ();
This number doesn't really make any sense. The standard library is only guaranteed to compare two thread IDs, and if the IDs are the same, the output will be the same, otherwise the results will be different. This is very useful in debugging and logging. As for the ID number, it doesn't make sense.
Manage thread ID for threads