How to create a new thread and wait for it in Windows 10 by C++?

与ChatGPT的对话

问:
How to create a new thread and wait for it in Windows 10 by C++?

答:
You can create a new thread and wait for it in Windows 10 using C++ by using the Windows API functions ‘CreateThread’ and ‘WaitForSingleObject’. Here’s an example of how you can do this:

#include <iostream>
#include <windows.h>

// Function that will be executed by the new thread
DWORD WINAPI ThreadFunction(LPVOID lpParam) {
    std::cout << "New thread is running." << std::endl;
    Sleep(3000); // Simulating some work in the thread
    return 0;
}

int main() {
    HANDLE hThread;
    DWORD threadId;

    // Create the new thread
    hThread = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadId);
    if (hThread == NULL) {
        std::cerr << "Failed to create thread." << std::endl;
        return 1;
    }

    // Wait for the thread to finish
    DWORD waitResult = WaitForSingleObject(hThread, INFINITE);
    if (waitResult == WAIT_FAILED) {
        std::cerr << "Failed to wait for thread." << std::endl;
        return 1;
    }

    std::cout << "Thread has finished." << std::endl;

    // Close the thread handle
    CloseHandle(hThread);

    return 0;
}

In this example:

1.CreateThread is used to create a new thread. The parameters are the security attributes (set to NULL for default), stack size (set to 0 for default), the thread function (ThreadFunction in this case), a parameter to pass to the thread function (set to NULL in this example), creation flags (set to 0 for default), and a pointer to a variable to receive the thread identifier.

2.WaitForSingleObject is used to wait for the thread to finish execution. It takes the thread handle and a timeout value (set to INFINITE to wait indefinitely).

3.‘CloseHandle’ is used to close the thread handle once we’re done with it to free up system resources.

Make sure to include windows.h header for Windows API functions and compile the code using a C++ compiler that supports Windows APIs.


2024.3.28更新:
ChatGPT所提供的代码经过我的实验被确认是有效的,接下来看看微软官方怎么说,来自:CreateThread function (processthreadsapi.h)

此函数用于创建一个在另一个进程的虚拟存储空间之内运行的进程。
语法规则:

HANDLE CreateThread(
  [in, optional]  LPSECURITY_ATTRIBUTES   lpThreadAttributes,
  [in]            SIZE_T                  dwStackSize,
  [in]            LPTHREAD_START_ROUTINE  lpStartAddress,
  [in, optional]  __drv_aliasesMem LPVOID lpParameter,
  [in]            DWORD                   dwCreationFlags,
  [out, optional] LPDWORD                 lpThreadId
);

参数:
1.[in, optional] lpThreadAttributes
这是一个指向SECURITY_ATTRIBUTES结构体的指针,用于确定是否所返回的句柄可以被子进程所继承。如果此参数为NULL,则句柄不可被继承。
SECURITY_ATTRIBUTES结构体中的lpSecurityDescriptor成员为新线程指定了一个安全描述符。如果lpThreadAttributes为NULL,则线程获得默认的安全描述符。
2.[in] dwStackSize
堆栈的初始大小,以字节为单位。系统将此值舍入到最近的页面。如果这个参数是0,新线程使用默认的执行大小。
3.[in] lpStartAddress
指向线程函数的指针。代表了线程的起始地址。
4.[in, optional] lpParameter
指向线程函数参数的指针。
5.[in] dwCreationFlags
控制线程创建的标志,具体的值如下:
在这里插入图片描述
6.[out, optional] lpThreadId
指向接收线程标识符的变量的指针。如果此参数为NULL,线程标识符是不会返回的。

返回值:
如果函数成功了,就会返回新线程的句柄。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值