线程ID与线程句柄的关系

Windows中创建线程的函数:CreateThread();该函数的原型声明如下:

HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, 
DWORD dwStackSize, 
LPTHREAD_START_ROUTINE lpStartAddress, 
LPVOID lpParameter, 
DWORD dwCreationFlags, 
LPDWORD lpThreadId); 

Parameters:

lpThreadAttributes
Ignored. Must be NULL.
NULL表示线程采用默认的安全性,我们进行编程是通常将该参数设置为NULL。

dwStackSize
Ignored. The default stack size for a thread is determined by the linker setting /STACK.
设置线程初始栈的大小,单位是字节。系统会把该参数四舍五入为最接近的页面大小。关于页面的概念,可以通过上网进行查询。该参数设置为0,表示默认采用与调用该函数的线程相同的栈空间大小。

lpStartAddress
Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc.
该参数为新线程的起始地址,该参数不能省略,其实,起始地址就是函数的名字。

lpParameter
Long pointer to a single 32-bit parameter value passed to the thread.
线程函数的命令行参数,如果没有命令行参数,该值应设置为NULL。

dwCreationFlags
Specifies flags that control the creation of the thread. 
线程创建标志,具体设置见下表:
Value Description
                CREATE_SUSPENDED The thread is created in a suspended state, and will not run until theResumeThread function is called.
                0 The thread runs immediately after creation.


lpThreadId
Long pointer to a 32-bit variable that receives the thread identifier.

If this parameter is NULL, the thread identifier is not returned. 

       LpThreadId是一个返回值,其值为线程ID,

      该函数的返回值为线程句柄,句柄是用户实现对线程操作的桥梁,线程ID 是线程在系统中存在的唯一标示。如果两个线程返回的ID相同,这说明两个线程是同一个线程。当然,在windows 2000和Windows NT4下,我们可以将该参数设置为NULL,标示对线程的ID不感兴趣,即不利用线程ID进行一定的操作。

      线程的句柄并不是线程的唯一标识,线程的句柄只是用来访问该线程的的一个32位值,尽管相同的句柄一定标识同一线程,但同一线程可能拥有两个打开的句柄,因此,不能用句柄来区分两个线程是否是同一线程。 


发布了29 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Atlas12345/article/details/45867379