c++线程(一)CreateEvent;CreateMutex()
#include <stdio.h>
#include <string> // for STL string class
#include <windows.h> // for HANDLE
#include <process.h> // for _beginthread()
#include <iostream>
using namespace std;
HANDLE g_event;
HANDLE ghMutex;
DWORD dwWaitResult01;
class ThreadX
{
private:
int loopStart;
int loopEnd;
int dispFrequency;
public:
string threadName;
ThreadX( int startValue, int endValue, int frequency )
{
loopStart = startValue;
loopEnd = endValue;
dispFrequency = frequency;
}
static unsigned __stdcall ThreadStaticEntryPoint(void * pThis)
{
ThreadX * pthX = (ThreadX*)pThis; //指针
pthX->ThreadEntryPoint();
return 1;
}
void ThreadEntryPoint()
{
//DWORD dwWaitResult00;
//Sleep(500);
// if (dwWaitResult00==WAIT_OBJECT_0)
//{
for (int i = loopStart; i <= loopEnd; ++i)
{
dwWaitResult01 = WaitForSingleObject(ghMutex,INFINITE);//获取权限
if (dwWaitResult01==WAIT_OBJECT_0)
{
if (i==300)
{
SetEvent( g_event );
cout<<"exit while"<<endl;
// Sleep(100);
}
/* if (i % dispFrequency == 0)
{*/
//printf( "%s: i = %d\n", threadName.c_str(), i );
printf( "%s: i = %d\n", threadName.c_str(), i );
// Sleep(100);
/*}*/
}
ReleaseMutex(ghMutex);//释放权限
}
// }
printf( "%s thread terminating\n", threadName.c_str() );
Sleep(100);
}
};
class ThreadFunction
{
public:
static unsigned __stdcall T(LPVOID m)
{
cout<<"start ThreadFunction"<<endl;
//Sleep(150);
int n=*(int*)m;
while (1)
{
dwWaitResult01 = WaitForSingleObject(ghMutex,INFINITE);
if (dwWaitResult01==WAIT_OBJECT_0)
{
cout<<"waite for signal criculate n="<<n<<endl;
// Sleep(100);
n++;
DWORD dwExit = WaitForSingleObject( g_event, 10 );
if (dwExit==WAIT_OBJECT_0)
//WaitForSingleObject(g_event,INFINITE)==WAIT_OBJECT_0
//g_event成功返回有信号状态
{
cout<<"exit criculate "<<endl;
// Sleep(100);
return 1;
}
}
ReleaseMutex(ghMutex);
}
return 0;
}
};
int main()
{
g_event=CreateEvent(NULL, TRUE, FALSE, NULL);//no signal
ghMutex = CreateMutex( NULL,FALSE,NULL);
if (ghMutex == NULL)
{
printf("CreateMutex error: %d\n", GetLastError());
return -1;
}
ThreadX * o1 = new ThreadX( 0, 200, 10 );
HANDLE ThreadF;
unsigned uiThread3ID;
//ThreadFunction *tf=new ThreadFunction();
int n=1;
ThreadF=(HANDLE)_beginthreadex(NULL,0,ThreadFunction::T,(LPVOID)&n,0,&uiThread3ID);
//ThreadF=(HANDLE)_beginthreadex(NULL,0,tf->T,(LPVOID)&n,0,&uiThread3ID);
HANDLE hth1;
unsigned uiThread1ID;
hth1 = (HANDLE)_beginthreadex( NULL,
0,
ThreadX::ThreadStaticEntryPoint,
o1,
CREATE_SUSPENDED, // so we can later call ResumeThread() no signal
&uiThread1ID );
if ( hth1 == 0 )
printf("Failed to create thread 1\n");
DWORD dwExitCode;
GetExitCodeThread( hth1, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259
printf( "initial thread 1 exit code = %u\n", dwExitCode );
o1->threadName = "t1";
ThreadX * o2 = new ThreadX( 0, 450, 15 );
HANDLE hth2;
unsigned uiThread2ID;
hth2 = (HANDLE)_beginthreadex( NULL,
0,
ThreadX::ThreadStaticEntryPoint,
o2,
CREATE_SUSPENDED,
&uiThread2ID );
if ( hth2 == 0 )
printf("Failed to create thread 2\n");
GetExitCodeThread( hth2, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259
printf( "initial thread 2 exit code = %u\n", dwExitCode );
o2->threadName = "t2";
ResumeThread( hth1 ); // serves the purpose of Jaeschke's t1->Start() set singal
ResumeThread( hth2 );
//ResumeThread( ThreadF );//有信号状态
WaitForSingleObject( hth1, INFINITE );//waite for signal change
WaitForSingleObject( hth2, INFINITE );
WaitForSingleObject( ThreadF, INFINITE );
GetExitCodeThread( hth1, &dwExitCode );
printf( "thread 1 exited with code %u\n", dwExitCode );
GetExitCodeThread( hth2, &dwExitCode );
printf( "thread 2 exited with code %u\n", dwExitCode );
CloseHandle( hth1 );
CloseHandle( hth2 );
//Sleep(500);
//SetEvent( g_event );
CloseHandle( ThreadF );
delete o1;
o1 = NULL;
delete o2;
o2 = NULL;
//delete tf;tf=NULL;
printf("Primary thread terminating.\n");
Sleep(500);
}