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

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

问题描述

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

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

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

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

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

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

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'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.

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

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