线程 ID 与线程句柄

发布于 2024-10-19 08:50:21 字数 53 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌无敌 2024-10-26 08:50:21

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

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

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

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

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

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.

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.

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.

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.

凌乱心跳 2024-10-26 08:50:21

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

因此,在内核空间中有一个 THREAD 类型的对象,ID = 12345

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

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

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.

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

And because you want to do something with the thread you have a pointer in your address space called a threadID with value 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.

口干舌燥 2024-10-26 08:50:21

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

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.

魔法唧唧 2024-10-26 08:50:21

大多数线程管理(或处理)都是通过线程句柄完成的,这是该函数的直接结果CreateThread。但是句柄(只是一个不透明指针)仅在给定进程的地址空间中有效。另一方面,特定线程的 ID 在整个系统中是唯一的。因此,可以通过调用

另一个可用于使用其 ID 直接作用于线程的函数是 PostThreadMessage

Most thread management (or handling) is done via the thread handle, which is the direct result of the function CreateThread. But the handle (being just an opaque pointer) is only valid in the address space of a given process. The ID of a particular thread, on the other hand, is unique across the entire system.It can therefore be used to obtain a handle by calling the OpenThread function.

Another function that can be used to act directly on a thread using its ID is PostThreadMessage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文