今天尝试编写多线程最简单的例子
#include "stdafx.h" #include "windows.h" #include <iostream> using namespace std; DWORD WINAPI Fun1Proc(LPVOID lpParamer); int main(int argc, char* argv[]) { HANDLE hThread; hThread=CreateThread(NULL,0,Fun1Proc,NULL,CREATE_SUSPENDED,NULL); ResumeThread(hThread); cout<<"Main Thread...."<<endl; CloseHandle(hThread); Sleep(20); return 0; } DWORD WINAPI Fun1Proc(LPVOID lpParamer) { cout<<"thread1 is running."<<endl; return 0; }
但输出结果不是自己想要的,他输出的乱序字符串。更改MD也不能解决,后来考虑是否是cout的输出问题。结果证实确实是vc6对C++标准支持的不完善。将cout换做printf的输出就没问题。。。
#include "stdafx.h" #include "windows.h" #include <iostream> using namespace std; DWORD WINAPI Fun1Proc(LPVOID lpParamer); int main(int argc, char* argv[]) { HANDLE hThread; hThread=CreateThread(NULL,0,Fun1Proc,NULL,CREATE_SUSPENDED,NULL); ResumeThread(hThread); printf("Main Thread...."); CloseHandle(hThread); Sleep(20); return 0; } DWORD WINAPI Fun1Proc(LPVOID lpParamer) { printf("thread1 is running."); return 0; }