windows 线程 ID 与线程句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5101918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:15:42  来源:igfitidea点击:

Thread ID vs. Thread Handle

windowslinuxmultithreadingoperating-system

提问by FireAphis

What is the difference between a thread ID and a thread handle? Why both are needed? Is there a difference between Windows and Linux?

线程 ID 和线程句柄有什么区别?为什么两者都需要?Windows 和 Linux 之间有区别吗?

回答by MarkR

Linux's pthread library does not, as far as I know, have a concept of a thread handle. pthread_create and other pthreads functions, return a thread ID.

据我所知,Linux 的 pthread 库没有线程句柄的概念。pthread_create 和其他 pthreads 函数,返回一个线程 ID。

Under Windows, the thread handle is different from the thread ID, in the same way that a file handle is different from a file name.

在 Windows 下,线程句柄不同于线程 ID,就像文件句柄不同于文件名一样。

The thread handle is a token which allows you to do something with the thread (typically wait for it or kill it). Win32 has these tokens for lots of objects, and calls them HANDLE in general.

线程句柄是一个令牌,它允许您对线程执行某些操作(通常是等待它或杀死它)。Win32 有许多对象的这些标记,并且通常将它们称为 HANDLE。

The token is essentially a pointer at the running (or stopped) thread and has a set of abilities associated with it, for example, you can have a handle which permits you to wait for, but not kill, a thread. In the same way, we can have a file handle which is read-only.

令牌本质上是一个指向正在运行(或停止)的线程的指针,并具有一组与之相关的能力,例如,您可以拥有一个句柄,允许您等待但不能终止线程。同样,我们可以有一个只读的文件句柄。

This level of indirection may or may not be useful, but it's the way Win32 does it, and it's broadly consistent with how it handles some other types of objects.

这种间接级别可能有用也可能没有用,但这是 Win32 的方式,并且与它处理某些其他类型的对象的方式大体一致。

回答by Ritsaert Hornstra

The ID is the unique numeric identifier of the thread running in the system. A thread handle, like any kernel object handle, can be seen as a special type of reference counted pointer to the kernel object.

ID 是系统中运行的线程的唯一数字标识符。一个线程句柄,就像任何内核对象句柄一样,可以被看作是一种特殊类型的指向内核对象的引用计数指针。

So in kernel space there is an object of type THREAD with ID = 12345

所以在内核空间中有一个 ID = 12345 的 THREAD 类型的对象

And because you want to do something with the thread you have a pointer in your address space called a threadID with value 44.

并且因为您想对线程执行某些操作,所以您的地址空间中有一个名为 threadID 且值为 44 的指针。

Please note that different handles to the same kernel object have different values (two pointers to one object) and that kernel objects can have handles in more than one process.

请注意,同一内核对象的不同句柄具有不同的值(指向一个对象的两个指针),并且内核对象可以在多个进程中具有句柄。

回答by Christian

Thread IDs are progressive (ie, one after another), which you can traverse. Thread handles, like most handles in Windows, are actually pointers. You might, for example, set thread property bits by using the thread handle - but not thread id.

线程 ID 是渐进式的(即一个接一个),您可以遍历它。线程句柄,就像 Windows 中的大多数句柄一样,实际上是指针。例如,您可以使用线程句柄来设置线程属性位 - 但不是线程 id。