11.3 Thread ID

Source: Internet
Author: User


Just as each process has a process ID, each thread has a thread ID. Unlike the process ID, which is unique within the system, the thread ID is only meaningful in the context of the process to which it belongs.
The process ID is represented by using the data type pid_t, which is a non-negative integer. The thread ID is stored using pthread_t, and the implementation can be stored using a structure, so the portable program cannot treat them as integers, so a comparison of thread IDs requires a specialized function implementation:

 
   
  
  1. #include <PTHREAD.H>
  2. Span class= "KWD" >int pthread_equal ( pthread_t tid1 pthread_t Tid2
  3. returns : nonzero if equal , Span class= "lit" >0 otherwise

Linux 3.2.0 uses unsigned long integers to store pthread_t types, and Solaris 10 uses unsigned integers to store pthread_t types. FreeBSD 8.0 and Mac OS X 10.6.8 Use the pthread pointer to store the pthread_t type.
One problem with using structs to store pthread_t data types to store thread IDs is that there are no portable methods to print the thread ID, and sometimes it is valid to print the thread ID in the debugger, but this is not usually required. The worst case scenario is that it produces non-portable debug code, so there's no limit to it.
A thread can obtain its own thread ID through the function pthread_self.

 
   
  
  1. #include <pthread.h>
  2. pthread_t pthread_self(void);
  3. Returns: the thread ID of the calling thread.

This function can be used with the function pthread_equal to identify whether a data structure identifies the current thread, for example, a main thread might schedule a task into a queue, and use the thread ID to control which task needs to be completed by which thread. This scenario illustrates 11.1: a master thread puts the task into a work queue. The thread pool with three worker threads removes the task from the queue, but not one thread can handle the task at the head of the task queue, and the master thread has assigned a specific thread for each job to process. Therefore, each job thread simply removes the task that marks its own thread ID.



From for notes (Wiz)

11.3 Thread ID

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.