线程 ID 与线程句柄 [英] Thread ID vs. Thread Handle

查看:22
本文介绍了线程 ID 与线程句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程 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?

推荐答案

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

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.

在 Windows 下,线程句柄与线程 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.

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

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.

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

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.

这篇关于线程 ID 与线程句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆