Open In App

Difference Between Thread ID and Thread Handle

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Thread in Operating System

Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated. The Thread get_id() method can be studied here where we can see how it’s used.

Thread Handle is a token that allows you to do something with the thread typically wait for it or kill it.

The CreateThread() API function creates a new thread for the process. It must specify the starting address of the new thread to be executed. The CreateThread() API returns simultaneously the thread handle and the thread ID. Operations such as creating thread objects are controlled by thread handle. At the system level, the thread ID uniquely identifies the thread when the specific thread it is running. 

Difference between Thread ID and Thread Handle:

  • The thread is operated by a thread handle. There may be multiple threads in which each thread handle can have different operation goals.
  • The value obtained by open thread is different from different processes.
  • Within the scope of the windows system, thread id is a unique identification of thread.
  • The thread handle is process-local while the thread id is system-wide.
  • To operate processes and threads between OS and Client, the thread handle acts as a bridge. The operating system has a form for maintaining the thread handle. The handle contains the reference count of the handle and related attributes.
  • Thread handle is used by the operating system that identifies processes and threads, users can use the handle to identify processes and threads and operate on them.
  • The thread ID is used by the operating system to identify processes and threads. The thread id is unique globally but the users can capture the thread handle of the process thread through this id.
  • A thread handle is a kernel object whereas the thread id is not.
  • For creating a thread id it will be automatically generated by the operating system when CreateThread is used.
  • The thread id and handle are two different things. The thread id uniquely corresponds in the windows system ie. if there are two threads and if they return the same thread id they must be the same thread otherwise they must originate from different threads.
  • The thread handle is a 32-bit value that is used to access a thread and it is not the only identifier of the thread.
  • The same handle must identify the same thread but the same thread may have 2 open handles so it cannot use the thread handle to distinguish whether the two threads are alike(same thread).

Last Updated : 26 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads