CreateThread函数的疑问
如果选择立即创建进程(参数为0),然后再CreateThread后面加一句printf,结果是先运行printf再进入线程函数,这是为什么代码如下
#include <stdio.h>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID lpParam) {
int i = 0;
while (i < 20) {
printf("I am form 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 whose Id = %d\nAnd whoes handle is %d \n", dwThreadId,hThread);
::WaitForSingleObject(hThread, INFINITE);
::CloseHandle(hThread);
return 0;
}
因为主线程和子线程是同步运行的,可能主线程运行地快了一点呗
(小问题:兄弟你英语不大好吧,应该是I am from而不是I am form啊)
页:
[1]