Manage thread ID for threads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.