#include "stdafx.h"
#include <iostream>
#include
<Windows.h>
using namespace std;
HANDLE hMutex;
DWORD
WINAPI Fun(LPVOID lp){
while(1){
WaitForSingleObject(hMutex,INFINITE);
cout<<"fun"<<endl;//如果不用信号量同步,则endl输出会不稳定,或者改成“fun\n”
Sleep(1000);
ReleaseMutex(hMutex);
}
}
int
main(){
HANDLE
hThread=CreateThread(NULL,0,Fun,NULL,0,NULL);
hMutex=CreateMutex(NULL,FALSE,"screen");//false为非本线程独占
CloseHandle(hThread);
while (1)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<"main"<<endl;
Sleep(1000);
ReleaseMutex(hMutex);
}
}
原文:http://www.cnblogs.com/duyy/p/3716752.html