多线程

#include "stdafx.h"
#include <windows.h>
#include <winbase.h>
#include <iostream.h>


DWORD WINAPI ThreadProc1(
  LPVOID lpParameter   // thread data
);


DWORD WINAPI ThreadProc2(
  LPVOID lpParameter   // thread data
);
int i=1;
HANDLE handle2;
HANDLE handle1;
HANDLE h_Mutex;




int main()
{
    
handle1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
if(handle1==NULL)
{
cout<<" Creat 1 ERROR!!!"<<endl;
}
handle2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
if(handle2==NULL)
{
cout<<" Creat 2 ERROR!!!"<<endl;
}


h_Mutex=CreateMutex(NULL,FALSE/*TRUE*/,NULL/*"m_Thread1"*/);
//  ReleaseMutex(h_Mutex);   
    
/* while(TRUE)
{
cout<<"Main Running !"<<endl;
}   */
 
Sleep(4000);
CloseHandle(handle1);
CloseHandle(handle2);


return 0;
}




DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{

while(i<100)
{
WaitForSingleObject(h_Mutex,INFINITE);
cout<<i++<<'\t'<<"MutiThread 1 Running !"<<endl;
ReleaseMutex(h_Mutex);
//    Sleep(0);
}
return 1;
}


DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{


while(i<100)
{
WaitForSingleObject(h_Mutex,INFINITE);
    cout<<i++<<'\t'<<"MutiThread 2 Running !"<<endl;
ReleaseMutex(h_Mutex);
// Sleep(0);
   
}
return 1;
}

你可能感兴趣的:(多线程)