// zzz.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
int workID=10;
int maxID=500000;
HANDLE hMutex;
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程1,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程2,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunThree(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程3,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunFour(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程4,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
int main(int argc, char* argv[])
{
hMutex=CreateMutex(NULL,false,"mutex");
HANDLE hThread1,hThread2,hThread3,hThread4;
hThread1=::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
hThread2=::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
hThread3=::CreateThread(NULL,0,ThreadFunThree,NULL,0,NULL);
hThread4=::CreateThread(NULL,0,ThreadFunFour,NULL,0,NULL);
while(true)
{
}
return 0;
}
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
int workID=10;
int maxID=500000;
HANDLE hMutex;
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程1,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程2,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunThree(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程3,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD __stdcall ThreadFunFour(LPVOID lParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(;;)
{
if(workID<maxID)
{
// Sleep(100);
workID+=1;
printf("当前工作线程4,%i\n",workID);
}
ReleaseMutex(hMutex);
}
return 0;
}
int main(int argc, char* argv[])
{
hMutex=CreateMutex(NULL,false,"mutex");
HANDLE hThread1,hThread2,hThread3,hThread4;
hThread1=::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
hThread2=::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
hThread3=::CreateThread(NULL,0,ThreadFunThree,NULL,0,NULL);
hThread4=::CreateThread(NULL,0,ThreadFunFour,NULL,0,NULL);
while(true)
{
}
return 0;
}