Thread:线,security:安全,attributes属性,Stack:堆栈,flag:标记
微软在Windows API中提供了建立新的线程的函数CreateThread,它的语法如下:
hThread = CreateThread (&security_attributes, dwStackSize, ThreadProc,pParam, dwFlags, &idThread) ;
第一个参数是指向SECURITY_ATTRIBUTES型态的结构的指针。在Windows 98中忽略该参数。在Windows NT中,它被设为NULL。第二个参数是用于新线程的初始堆栈大小,默认值为0。在任何情况下,Windows根据需要动态延长堆栈的大小。
CreateThread的第三个参数是指向线程函数的指针。函数名称没有限制,但是必须以下列形式声明:
DWORD WINAPI ThreadProc (PVOID pParam) ;
CreateThread的第四个参数为传递给ThreadProc的参数。这样主线程和从属线程就可以共享数据。
CreateThread的第五个参数通常为0,但当建立的线程不马上执行时为旗标CREATE_SUSPENDED。线程将暂停直到呼叫ResumeThread来恢复线程的执行为止。第六个参数是一个指标,指向接受执行绪ID值的变量。
CreateThread函数原型如下:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId);
Parameters
lpThreadAttributes
Ignored. Can be NULL.
dwStackSize
Ignored. The default stack size for a thread is determined by the linker setting /STACK.
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.
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 the ResumeThread 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.
Return Values
A handle to the new thread indicates success. NULL indicates failure. To get extended error information, call GetLastError.