// MyThread.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
HANDLE hMutex;
DWORD MyThread1(VOID)
{
INT32 i = 0;
while(1) {
WaitForSingleObject(hMutex, INFINITE);
printf("1:%d/n",i++);
ReleaseMutex(hMutex);
}
return 0;
}
DWORD MyThread2(VOID)
{
INT32 i = 0;
while(1) {
WaitForSingleObject(hMutex, INFINITE);
printf("2:%d/n",i++);
ReleaseMutex(hMutex);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread1;
HANDLE hThread2;
hMutex = CreateMutex(NULL,FALSE,NULL);
if(NULL == hMutex) {
printf("Create hMutex error/n");
return -1;
}
hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThread1, NULL, 0, NULL);
if(hThread1) {
printf("CreateThread MyThread1 success/r/n");
} else {
printf("CreateThread MyThread1 error/r/n");
return -1;
}
hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThread2, NULL, 0, NULL);
if(hThread2) {
printf("CreateThread MyThread2 success/r/n");
} else {
printf("CreateThread MyThread2 error/r/n");
return -1;
}
while(1) {
Sleep(1000);
}
CloseHandle(hThread1);
CloseHandle(hThread2);
return 0;
}
