Click here to Skip to main content
16,012,107 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Extract information from a List class (Win32 API question) Pin
Leprechaun786-Nov-06 23:22
Leprechaun786-Nov-06 23:22 
GeneralRe: Extract information from a List class (Win32 API question) Pin
David Crow7-Nov-06 2:49
David Crow7-Nov-06 2:49 
GeneralRe: Extract information from a List class (Win32 API question) Pin
Leprechaun787-Nov-06 3:55
Leprechaun787-Nov-06 3:55 
GeneralRe: Extract information from a List class (Win32 API question) Pin
David Crow7-Nov-06 4:03
David Crow7-Nov-06 4:03 
GeneralRe: Extract information from a List class (Win32 API question) Pin
Leprechaun788-Nov-06 2:16
Leprechaun788-Nov-06 2:16 
GeneralRe: Extract information from a List class (Win32 API question) Pin
David Crow8-Nov-06 2:47
David Crow8-Nov-06 2:47 
GeneralRe: Extract information from a List class (Win32 API question) Pin
Leprechaun788-Nov-06 3:15
Leprechaun788-Nov-06 3:15 
Questionpuzzled by the following calss defination Pin
cy163@hotmail.com6-Nov-06 3:31
cy163@hotmail.com6-Nov-06 3:31 
I saw the puzzling class defination in the reference--
http://msdn2.microsoft.com/en-us/library/efk30beh(VS.80).aspx[^]

My question comes from the below line

***********************
CPrimeTest() : m_pCalcNext( new CEvent( FALSE, FALSE ) ) , m_pCalcFinished( new CEvent( FALSE, FALSE ) ) , m_pTerminateThread( new CEvent( FALSE, FALSE ) ) , m_iCurrentPrime( 0 ). If so, why not put them in the body of the class defination.
*******************************

-- How to explain it.

-- Can we treat 'm_pCalcNext', 'm_pCalcFinished' 'm_pTerminateThread' and 'm_iCurrentPrime' as four member variables of public type of the class
<br />
<br />
CPrimeTest { <br />
public: <br />
	<br />
	{ <br />
		// Create a thread that will calculate the prime numbers <br />
		CWinThread* pThread; <br />
		pThread = ::AfxBeginThread( PrimeCalcProc, this, 0, 0, CREATE_SUSPENDED, NULL); <br />
		pThread->m_bAutoDelete = FALSE; <br />
		pThread->ResumeThread(); <br />
		// Calcuate the first 10 prime numbers in the series on the thread <br />
		for( UINT i = 0; i < 10; i++ ) <br />
		{ <br />
			// Signal the thread to do the next work item <br />
			m_pCalcNext->SetEvent(); <br />
			// Wait for the thread to complete the current task <br />
			::WaitForSingleObject( m_pCalcFinished->m_hObject, INFINITE ); <br />
			// Print the result <br />
			TRACE( "The value of m_iCurrentPrime is: %d\n", m_iCurrentPrime ); <br />
		} <br />
		// Notify the worker thread to exit and wait for it to complete m_pTerminateThread->SetEvent(); <br />
		::WaitForSingleObject( pThread->m_hThread, INFINITE ); <br />
		delete pThread; } <br />
	<br />
	~CPrimeTest() <br />
	{ <br />
		delete m_pCalcNext; <br />
		delete m_pCalcFinished; <br />
		delete m_pTerminateThread; <br />
	} <br />
private: <br />
	// Determines whether the given number is a prime number static <br />
	BOOL IsPrime( INT ThisPrime ) <br />
	{ <br />
		if( ThisPrime < 2 ) return FALSE; <br />
		for( INT n = 2; n < ThisPrime; n++ ) <br />
		{ <br />
			if( ThisPrime % n == 0 ) <br />
				<br />
				return FALSE; <br />
		} <br />
		return TRUE; <br />
	} <br />
	// Calculates the next prime number in the series static <br />
	INT NextPrime( INT ThisPrime ) <br />
	{ while( TRUE ) <br />
	{ if( IsPrime( ++ThisPrime ) ) <br />
	{ return ThisPrime; } } } <br />
	// Worker thread responsible for calculating the next prime <br />
	// number in the series <br />
	static UINT __cdecl PrimeCalcProc( LPVOID lpParameter ) <br />
	{ CPrimeTest* pThis = static_cast<CPrimeTest*>( lpParameter ); <br />
	VERIFY( pThis != NULL ); VERIFY( pThis->m_pCalcNext != NULL ); <br />
	VERIFY( pThis->m_pCalcFinished != NULL ); <br />
	VERIFY( pThis->m_pTerminateThread != NULL ); <br />
	// Create a CMultiLock object to wait on the various events <br />
	// WAIT_OBJECT_0 refers to the first event in the array, WAIT_OBJECT_0+1 refers to the second <br />
	CSyncObject* pWaitObjects[] = { pThis->m_pCalcNext, pThis->m_pTerminateThread }; <br />
	CMultiLock MultiLock( pWaitObjects, 2L ); <br />
	while( MultiLock.Lock( INFINITE, FALSE ) == WAIT_OBJECT_0 ) <br />
	{ <br />
		// Calculate next prime <br />
		pThis->m_iCurrentPrime = NextPrime( pThis->m_iCurrentPrime ); <br />
		// Notify main thread calculation is complete <br />
		pThis->m_pCalcFinished->SetEvent(); <br />
	} <br />
	// Terminate the thread <br />
	::AfxEndThread( 0, FALSE ); <br />
	return 0L; <br />
	} CEvent* m_pCalcNext; <br />
	// notifies worker thread to calculate next prime <br />
	CEvent* m_pCalcFinished; <br />
	// notifies main thread current calculation is complete <br />
	CEvent* m_pTerminateThread; <br />
	// notifies worker thread to terminate INT m_iCurrentPrime; <br />
	// current calculated prime number <br />
};<br />
<br />
<br />
<br />
<br />

AnswerRe: puzzled by the following calss defination Pin
Cedric Moonen6-Nov-06 3:37
Cedric Moonen6-Nov-06 3:37 
GeneralRe: puzzled by the following calss defination Pin
led mike6-Nov-06 4:24
led mike6-Nov-06 4:24 
GeneralRe: puzzled by the following calss defination Pin
Cedric Moonen6-Nov-06 4:38
Cedric Moonen6-Nov-06 4:38 
GeneralRe: puzzled by the following calss defination Pin
led mike6-Nov-06 4:54
led mike6-Nov-06 4:54 
AnswerRe: puzzled by the following calss defination Pin
toxcct6-Nov-06 5:03
toxcct6-Nov-06 5:03 
QuestionEntrypoint problem in my project Pin
Raghavendra Pise6-Nov-06 3:12
Raghavendra Pise6-Nov-06 3:12 
AnswerRe: Entrypoint problem in my project Pin
Mark Salsbery6-Nov-06 6:55
Mark Salsbery6-Nov-06 6:55 
QuestionSetDiBitsToDevice() Pin
Waldermort6-Nov-06 3:06
Waldermort6-Nov-06 3:06 
AnswerRe: SetDiBitsToDevice() Pin
Chris Losinger6-Nov-06 3:19
professionalChris Losinger6-Nov-06 3:19 
GeneralRe: SetDiBitsToDevice() Pin
Waldermort6-Nov-06 3:35
Waldermort6-Nov-06 3:35 
GeneralRe: SetDiBitsToDevice() Pin
Waldermort6-Nov-06 4:11
Waldermort6-Nov-06 4:11 
Questionhow to call a function a scheduled time? Pin
Banks K6-Nov-06 3:03
Banks K6-Nov-06 3:03 
AnswerRe: how to call a function a scheduled time? Pin
benjymous6-Nov-06 3:24
benjymous6-Nov-06 3:24 
AnswerRe: how to call a function a scheduled time? Pin
David Crow6-Nov-06 3:59
David Crow6-Nov-06 3:59 
AnswerRe: how to call a function a scheduled time? Pin
led mike6-Nov-06 4:27
led mike6-Nov-06 4:27 
AnswerRe: how to call a function a scheduled time? Pin
prasad_som6-Nov-06 19:05
prasad_som6-Nov-06 19:05 
AnswerRe: how to call a function a scheduled time? Pin
Blake Miller7-Nov-06 3:53
Blake Miller7-Nov-06 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.