Thread handles and thread IDs


The CreateThread() API is used to create threads. The API returns both a thread handle and a thread identifier (ID). The thread handle has full access rights to the thread object created. The thread ID uniquely identifies the thread at the system level while the thread is running. This relationship is similar to that of the process handle and the process ID. The thread ID can be recycled after the thread exits or is terminated.

On Windows 95, Windows 98, and Windows NT, there is no way to obtain the thread handle from the thread ID. On these operating systems, a thread handle can be shared with another process by using the DuplicateHandle() function.

On Windows Millennium Edition, Windows 2000, and Windows XP, the OpenThread() function can be used to obtain a thread handle based on the thread ID.

The APIs that take a thread handle can be used to suspend and resume a thread, adjust the priority of a thread, read and write a thread's registers, limit a thread to a set of processors, terminate a thread, and other actions. Performing any one of these operations on a thread without the knowledge of the owning process is dangerous, and may cause the process to fail.

NOTE: On all Win32 operating systems, you can also take full control of the application by calling DebugActiveProcess(). Debuggers receive the thread handles for a process when the threads are created. These handles have THREAD_GET_CONTEXT, THREAD_SET_CONTEXT, and THREAD_SUSPEND_RESUME access to the thread.