Winapi 在Win32中创建线程

Winapi 在Win32中创建线程,winapi,multithreading,Winapi,Multithreading,ThreadFunc()在这里会被调用两次吗?有时我只注意到一个电话,有时一个也没有 #include <windows.h> #include <stdio.h> DWORD WINAPI ThreadFunc(LPVOID); int main() { HANDLE hThread; DWORD threadld; hThread = CreateThread(NULL, 0, ThreadFunc, 0, 0, &threadl

ThreadFunc()在这里会被调用两次吗?有时我只注意到一个电话,有时一个也没有

#include <windows.h>
#include <stdio.h>

DWORD WINAPI ThreadFunc(LPVOID);

int main()
{
    HANDLE hThread;
    DWORD threadld;

    hThread = CreateThread(NULL, 0, ThreadFunc, 0, 0, &threadld );
    printf("Thread is running\n");
}

DWORD WINAPI ThreadFunc(LPVOID p)
{
    printf("In ThreadFunc\n");
    return 0;
}
输出2

Thread is running
In ThreadFunc
Press any key to continue . . .
输出3

Thread is running
Press any key to continue . . .

为了调用CRT函数,例如
printf
,您应该使用或而不是
CreateThread


无论如何,程序可能在线程有机会输出任何内容之前结束。

为了调用CRT函数,例如
printf
,您应该使用或而不是
CreateThread


无论如何,程序可能会在线程有机会输出任何内容之前结束。

一点添加:在main()内使用WaitForSingleObject来完成线程的作业。

一点添加:在main()内使用WaitForSingleObject来完成线程的作业。

不,ThreadFunc永远不会被调用两次。无论如何,我相信您的代码片段是不完整的-您能将完整的代码片段发布到您发现此问题的地方吗?

不,ThreadFunc不应该被调用两次。在任何情况下,我相信您的代码片段是不完整的-您能将完整的代码片段发布到您发现此问题的地方吗?

遵循此链接:遵循此链接:
Thread is running
Press any key to continue . . .