#include<iostream>
#include<windows.h>
using namespace std;
HANDLE hMutex;
DWORD WINAPI threadFun(LPVOID lpParamter){
char * p = (char *)lpParamter;
while(1){
WaitForSingleObject(hMutex,INFINITE);
cout<<p<<endl;
Sleep(1000);
ReleaseMutex(hMutex);
}
}
int main(){
char buffer[10]="in thread";
HANDLE hThread = CreateThread(NULL,0,threadFun,buffer,0,NULL);
hMutex = CreateMutex(NULL,FALSE,"screen");
CloseHandle(hThread);
while(1){
WaitForSingleObject(hMutex,INFINITE);
cout<<"in main"<<endl;
Sleep(1000);
ReleaseMutex(hMutex);
}
return 0;
}
#include<iostream>
#include<windows.h>
using namespace std;
DWORD WINAPI threadFun(LPVOID lpParamter){
while(1){
cout<<"in thread\n";
Sleep(2000);
}
}
int main(){
HANDLE hThread = CreateThread(NULL,0,threadFun,NULL,0,NULL);
CloseHandle(hThread);
while(1){
cout<<"in main\n";
Sleep(2000);
}
return 0;
}
#include<windows.h>
#include<stdio.h>
int main(){
HANDLE hMutex = CreateMutex(NULL,FALSE,"mis");
if(GetLastError() == ERROR_ALREADY_EXISTS){
printf("have existed\n");
CloseHandle(hMutex);
hMutex=NULL;
system("pause");
return 0;
}
printf("hello world!\n");
Sleep(5000);
system("pause");
return 0;
}