程序源码
#include "stdio.h"
#include "windows.h"
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
int i = 0;
while(i<20)
{
printf("I am from a Thread,count=%d\n",i++);
}
return 0;
}
int main(int argc,char *argv[])
{
HANDLE hThread;
DWORD dwThreadId;
hThread = ::CreateThread(NULL,NULL,ThreadProc,NULL,0,&dwThreadId);
printf("Now another thread has been created.ID=%d\n",dwThreadId);
WaitForSingleObject(hThread,INFINITE);
::CloseHandle(hThread);
return 0;
}
结果:
为什么运行结果会打印两行Now another thread has been created.ID=10400按照程序只有一条printf语句啊,先将新建线程的ID号打印出来然后主线程等待,直到新建线程的线程函数ThreadProc执行完后在回到主线程。但为什么程序会打印出两行Now another thread has been created.ID=10400。按道理应当只打印一次才对啊,不解,求指教。