Example #1
0
int 
main(int argc, 
	 char *argv[] )
{
	int increment=10;

	CIncrementThread *pThreadInc;

	cout << "\n****** T H R E A D  T E S T *********\n\n";

	cout << "--INSTANTIATING CINCREMENTTHREAD DERIVED FROM CTHREAD\n";
	pThreadInc = new CIncrementThread;

	if( pThreadInc )
	{
		if( pThreadInc->PingThread(500) )
		{
			cout << "\nSENDING EVENT + 10\n";
			pThreadInc->Event((LPVOID)&increment);
			Sleep(500);
			cout << "\nSENDING EVENT ++\n";
			pThreadInc->Event();
			Sleep(500);
		}
		else {
			cout << "\nTHREAD DID NOT START\n";
			exit(0);
		}
		cout << "\n--CHANGING THREAD MODEL TO INTERVAL BASED\n";
		pThreadInc->SetThreadType(ThreadTypeIntervalDriven,100);
		Sleep(500);
		cout << "\n--CHANGING THREAD MODEL BACK TO EVENT DRIVEN\n";
		pThreadInc->SetThreadType(ThreadTypeEventDriven);
		Sleep(500);
		cout << "\n--SENDING + 10\n";
		pThreadInc->Event((LPVOID)&increment);
		Sleep(500);
		cout << "\n--DESTROYING OBJECT\n";
		delete pThreadInc;
		Sleep(500);
	}
	else
	{
		cout << "FAILED TO ALLOCATE THREAD\n";
		exit(0);
	}

	cout << "\n--INSTANTIATING AN ARRAY OF TEN CINCREMENTTHREADS DERIVED FROM CTHREAD\n";
	pThreadInc = new CIncrementThread [10];
	if( pThreadInc )
	{
		if( pThreadInc[9].PingThread(2000) )
		{
			for(int i=0; i<10 ; i++ )
			{
				pThreadInc[i].Event();
			}
			Sleep(500);
			cout << "\n\n--DESTROYING OBJECT ARRAY\n";
			delete [] pThreadInc;
		}
		else {
			cout << "\nTHREADS DID NOT START\n";
			exit(0);
		}
	}
	else {
		cout << "FAILED TO ALLOCATE THREADS\n";
		exit(0);
	}

	cout << "\n--INSTANTIATING A CINCRMENTTHREAD BY DECLARATION\n";
	{
		CIncrementThread th;

		if( th.PingThread(2000) )
		{
			cout << "\nSENDING EVENT\n";
			th.Event();
			Sleep(500);
		}
		else
		{
			cout << "FAILED TO START THREAD\n";
			exit(0);
		}
		cout << "\nLEAVING SCOPE\n";
		
	}

    cout << "\n--INSTANTIATING A CINCREMENTTHREAD BY DECLARATION\n";
	{
		CIncrementThread MyThread; // object allocated and thread started
		int two = 2;
		while( MyThread.GetValue() < 20 )
		{
			MyThread.Event();  // increment value by one
			Sleep(100);        // pauses the root thread for 100 milli-seconds
		}

		MyThread.Reset();
		while( MyThread.GetValue() < 40 )
		{
			MyThread.Event(&two);
			Sleep(100);
		}

	}

	cout << "--INSTANTIATING THE ORIGIONAL CTHREAD CLASS BY DECLARATION\n";
	{
		MyTaskClass myTask;
		CThread thread;
		thread.Event(&myTask);
		while( myTask.Status() != TaskStatusCompleted )
			Sleep(500);

		thread.Event(&myTask);
		myTask.Wait(1);
	}

	cout << "\n--INSTANTIATING THE ORIGIONAL CTHREAD CLASS BY DECLARATION\n";
	{
		MyTaskClass *pMyTask = new MyTaskClass;

		CThread thread;
		thread.Event(pMyTask);
		if( pMyTask->Wait(1) )
		{
			cout << "\tfreeing pMyTask\n";
			delete pMyTask;
		}
	}

	cout << "\n--INSTANTIATING a CTHREAD AND USING A CTASK OBJECT\n";
	{
		CTaskIncrementer incr;
		CThread thr;

		incr.SetIncr(2);
		while( incr.GetValue() < 40 ) {
			thr.Event(&incr);
			Sleep(100);
		}
	}
	cout << "\ndone\n";

}