C++多线程编程(入门实例)
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hMutex = NULL;
DWORD WINAPI Fun(LPVOID lpParamter)
{
for (int i = 0; i < 10; i++)
{
WaitForSingleObject(hMutex, INFINITE);
cout << "A Thread Fun Display!" << endl;
Sleep(100);
ReleaseMutex(hMutex);
}
return 0L;
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
hMutex = CreateMutex(NULL, FALSE,"screen");
CloseHandle(hThread);
for (int i = 0; i < 10; i++)
{
WaitForSingleObject(hMutex,INFINITE);
cout << "Main Thread Display!" << endl;
Sleep(100);
ReleaseMutex(hMutex);
}
return 0;
}