网站首页  词典首页

请输入您要查询的函数:

 

术语 createthread
释义 CreateThread
语法:
C++
HANDLE WINAPI CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in_opt LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out_opt LPDWORD lpThreadId
);
CreateThread函数
创建一个线程来执行在调用进程的虚拟地址空间。
要创建一个线程,在另一个进程的虚拟地址空间中运行,使用CreateRemoteThread的功能。
参数
lpThreadAttributes [中,可选]
一个 SECURITY_ATTRIBUTES结构,决定是否返回的句柄可以被继承的子进程的指针。如果lpThreadAttributes为NULL,则句柄不能被继承。
结构的lpSecurityDescriptor成员指定一个新的线程安全描述符。如果lpThreadAttributes为NULL,线程获得一个默认的安全描述符。在对来自线程默认安全描述符的ACL的主要创造者的象征。
Windows XP/2000操作系统:在对来自线程默认安全描述符的ACL或模拟的主要创造者的象征。与Windows SP2和Windows Server 2003的XP的改变了这种行为。有关更多信息,请参见备注。
dwStackSize [in]
堆栈以字节为单位的初始大小。该系统四轮到最近的网页的价值。如果此参数为0,新的线程使用的可执行文件的默认大小。有关更多信息,请参见线程堆栈大小。
lpStartAddress [in]
该申请的指针定义的函数被执行的线程。这个指针代表了线程的起始地址。欲了解有关线程函数的详细信息,请参阅ThreadProc。
lpParameter [中,可选]
对变量的指针传递给线程。
dwCreationFlags [in]
这些标志来控制创建线程。
ValueMeaning
0The线程创建后立即运行。
CREATE_SUSPENDED
0x00000004The线程创建处于挂起状态,并不直到ResumeThread函数被调用运行。
STACK_SIZE_PARAM_IS_A_RESERVATION
0x00010000The dwStackSize参数指定堆栈的初始储备规模。如果此标志没有被指定,dwStackSize指定提交的大小。
视窗2000:STACK_SIZE_PARAM_IS_A_RESERVATION标志不支持。
lpThreadId [指出,可选]
一个变量,它接收线程标识符的指针。如果该参数为NULL,线程标识符不返回。
返回值
如果函数成功,返回值是一个句柄新线程。
如果函数失败,返回值为NULL。为了获得更多错误信息,调用GetLastError。
请注意,CreateThread可能会成功,即使lpStartAddress点的数据,代码,或无法访问。如果起始地址是无效的线程运行时,就会发生异常,以及线程终止。线程终止由于起始地址是无效的处理作为线程的进程的错误退出。此行为是类似于CreateProcess的,异步的性质在创建的过程,即使它是指无效或丢失的动态链接库(DLL)。
备注
该进程的线程可以创建数目有限的可用虚拟内存。默认情况下,每个线程都有一个兆字节的堆栈空间。因此,您可以创建最多2048个线程。如果您降低默认的堆栈大小,可以创建更多的线程。但是,您的应用程序将有更好的表现,如果您创建一个线程,每个处理器的请求队列建立的该应用程序维护的上下文信息。一个线程在处理过程中会在未来的队列中的请求队列中的所有要求。
新创建的线程句柄与THREAD_ALL_ACCESS访问权限。如果没有安全描述符时所提供的线程创建,默认的安全描述,构造新的主要使用线程的进程正在创建线程标记。当呼叫者尝试访问与OpenThread功能线程,有效地调用令牌进行评估,而这种安全描述符授予或拒绝访问。
Windows XP/2000操作系统:如果没有安全描述符时所提供的线程创建,默认的安全描述,构造,使用有效的线程标记。如果线程是假冒其他用户,线程的有效凭证是模拟令牌和默认的安全描述符只允许访问模拟令牌的TokenDefaultDacl所有者或成员。如果线程没有假冒其他用户,线程的有效凭证是其主要令牌。此行为改变开始,带有SP2的Windows和Windows Server 2003 XP中。有关更多信息,请参见线程安全和访问权限。
新创建的线程必须充分利用自己的权利时,调用GetCurrentThread功能。
服务器2003和Windows XP/2000:线程的访问权限,本身都是通过评估计算的Windows的首要其中对线程的默认安全描述符构建线程创建进程令牌。如果线程在远程过程中产生,主要是远程进程令牌使用。因此,新创建的线程可能会减少自己的访问权限时调用GetCurrentThread。一些访问权限,包括THREAD_SET_THREAD_TOKEN和THREAD_GET_CONTEXT可能不存在,导致意想不到的失败。为此,创建一个线程,而假冒其他用户不建议。
该线程在开始执行的lpStartAddress参数指定的函数。如果这个函数返回时,DWORD的返回值是用来终止隐式调用的线程的ExitThread函数。使用GetExitCodeThread函数来获取线程的返回值。
该线程是创建一个THREAD_PRIORITY_NORMAL线程优先级。使用GetThreadPriority和SetThreadPriority函数来获取和设置线程的优先级值。
当一个线程终止时,该线程对象年满信号状态,满足了任何线程对象上等待。
该线程对象仍然有效,直到该线程终止,所有句柄它已通过对CloseHandle调用封闭系统中。
PRB:ExitProcess期间,ExitThread,CreateThread,CreateRemoteThread的功能,这一过程已经开始(作为一个由调用CreateProcess的结果)是序列彼此之间在一个过程。只有对这些事件可以发生在一个地址空间一次。这意味着,持有下列限制:
在进程启动和DLL初始化例程,新的线程可以被创建,但他们没有开始执行,直到DLL初始化是完成这一进程。
只有一个线程在一个进程可以在DLL初始化或分离一次例行。
PRB:ExitProcess期间未完成之前,都是以DLL初始化例程或分离不线程。
一个可执行文件,调用线程C运行时库(CRT)应该使用,而不是CreateThread和ExitThread的线程管理_beginthreadex和_endthreadex职能,这需要多使用CRT的线程版本。如果一个线程创建使用CreateThread调用CRT显示器,CRT的可终止在低内存条件的过程。
实例
有关示例,请参阅创建线程。
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
参见
CloseHandle
CreateProcess的
CreateRemoteThread的
PRB:ExitProcess期间
ExitThread
GetExitCodeThread
GetThreadPriority
进程和线程函数
ResumeThread
SetThreadPriority
SECURITY_ATTRIBUTES
SuspendThread
ThreadProc
主题
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月27日
==英文原文==CreateThread Function
Creates a thread to execute within the virtual address space of the calling process.
To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.
Syntax
C++
HANDLE WINAPI CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in_opt LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out_opt LPDWORD lpThreadId
);
Parameters
lpThreadAttributes [in, optional]
A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. The ACLs in the default security descriptor for a thread come from the primary token of the creator.
Windows XP/2000: The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator. This behavior changed with Windows XP with SP2 and Windows Server 2003. For more information, see Remarks.
dwStackSize [in]
The initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information, see Thread Stack Size .
lpStartAddress [in]
A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the thread. For more information on the thread function, see ThreadProc .
lpParameter [in, optional]
A pointer to a variable to be passed to the thread.
dwCreationFlags [in]
The flags that control the creation of the thread.
ValueMeaning
0The thread runs immediately after creation.
CREATE_SUSPENDED
0x00000004The thread is created in a suspended state, and does not run until the ResumeThread function is called.
STACK_SIZE_PARAM_IS_A_RESERVATION
0x00010000The dwStackSize parameter specifies the initial reserve size of the stack. If this flag is not specified, dwStackSize specifies the commit size.
Windows 2000: The STACK_SIZE_PARAM_IS_A_RESERVATION flag is not supported.

lpThreadId [out, optional]
A pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.
Return Value
If the function succeeds, the return value is a handle to the new thread.
If the function fails, the return value is NULL. To get extended error information, call GetLastError .
Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess , where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
Remarks
The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,048 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.
The new thread handle is created with the THREAD_ALL_ACCESS access right. If a security descriptor is not provided when the thread is created, a default security descriptor is constructed for the new thread using the primary token of the process that is creating the thread. When a caller attempts to access the thread with the OpenThread function, the effective token of the caller is evaluated against this security descriptor to grant or deny access.
Windows XP/2000: If a security descriptor is not provided when the thread is created, a default security descriptor is constructed using the effective token of the thread. If the thread is impersonating another user, the thread's effective token is the impersonation token and the default security descriptor allows access only to the impersonation token's TokenDefaultDacl owner or members. If the thread is not impersonating another user, the thread's effective token is its primary token. This behavior changed starting with Windows XP with SP2 and Windows Server 2003. For more information, see Thread Security and Access Rights .
The newly created thread has full access rights to itself when calling the GetCurrentThread function.
Windows Server 2003 and Windows XP/2000: The thread's access rights to itself are computed by evaluating the primary token of the process in which the thread was created against the default security descriptor constructed for the thread. If the thread is created in a remote process, the primary token of the remote process is used. As a result, the newly created thread may have reduced access rights to itself when calling GetCurrentThread. Some access rights including THREAD_SET_THREAD_TOKEN and THREAD_GET_CONTEXT may not be present, leading to unexpected failures. For this reason, creating a thread while impersonating another user is not recommended.
The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.
The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.
When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.
The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle .
The ExitProcess , ExitThread , CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:
During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.
Only one thread in a process can be in a DLL initialization or detach routine at a time.
ExitProcess does not complete until there are no threads in their DLL initialization or detach routines.
A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
Examples
For an example, see Creating Threads .
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
See Also
CloseHandle
CreateProcess
CreateRemoteThread
ExitProcess
ExitThread
GetExitCodeThread
GetThreadPriority
Process and Thread Functions
ResumeThread
SetThreadPriority
SECURITY_ATTRIBUTES
SuspendThread
ThreadProc
Threads
Send comments about this topic to Microsoft
Build date: 8/27/2009
==原始网址==http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 11:30:35