VC++:CreateThread建立新的线程的函数

本文详细介绍了Windows API中的CreateThread函数,包括其参数说明及返回值解释。解析了如何使用此函数创建新线程,并介绍了线程状态控制标志CREATE_SUSPENDED的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值