Click here to Skip to main content
15,916,463 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++ Multiple inheritance Pin
ScotDolan14-May-07 7:41
ScotDolan14-May-07 7:41 
GeneralRe: C++ Multiple inheritance Pin
Cedric Moonen14-May-07 7:52
Cedric Moonen14-May-07 7:52 
GeneralRe: C++ Multiple inheritance Pin
ScotDolan14-May-07 8:26
ScotDolan14-May-07 8:26 
GeneralRe: C++ Multiple inheritance Pin
Mark Salsbery14-May-07 8:18
Mark Salsbery14-May-07 8:18 
GeneralRe: Mark Has a, Does a, Where is a, Pin
ScotDolan14-May-07 8:35
ScotDolan14-May-07 8:35 
GeneralRe: Mark Has a, Does a, Where is a, Pin
Mark Salsbery14-May-07 9:01
Mark Salsbery14-May-07 9:01 
GeneralRe: Mark Has a, Does a, Where is a, Pin
led mike14-May-07 9:39
led mike14-May-07 9:39 
GeneralThe purpose of the thread class Pin
ScotDolan14-May-07 10:56
ScotDolan14-May-07 10:56 
The purpose of the thread class is to make simple functions that consolidate, hide all the mondain task that need to be performed to start, kill, suspend, resume, a thread. The object is to try to prevent any Operating System specific code from getting into the my mainloop and poll-lenze code. This will make it easier for me to port the code over to Linux, Unix or any other os that supports C++. This is why the i use ThreadEnter(),ThreadRun(), and ThreadExit() can all be override. Because, ThreadEnter(),ThreadRun(), and ThreadExit() get called in the worker thread. other object is to keep the create a spreate function where the thread code goes.

The end results is that when it comes time to port the code over to a new Os, i should only have to rewrite ClxThread.


<br />
#pragma once<br />
<br />
<br />
class ClxThread<br />
{<br />
public:<br />
	ClxThread();<br />
	virtual ~ClxThread();<br />
<br />
	typedef unsigned (__stdcall *PTHREADFUNC)(void *);<br />
	//Thread Management<br />
	bool CreateNewThread(void);<br />
	bool CreateNewThread(PTHREADFUNC pThreadFunc);<br />
	bool Wait(); //Wait for thread to end<br />
	bool Suspend(); //Suspend the thread<br />
	bool Resume(); //Resume a suspended thread<br />
	bool Kill(); //Terminate a thread<br />
	bool IsActive(); //Check for activity<br />
<br />
	//override these functions in the derived class<br />
	virtual void ThreadEntry(){ }<br />
	virtual void ThreadExit(){ }<br />
	virtual void ThreadRun(){ }<br />
<br />
	//a friend<br />
	//friend DWORD WINAPI _ThreadFunc(LPVOID  pvThread);<br />
	static UINT  WINAPI _ThreadFunc(LPVOID pParam);<br />
<br />
public:<br />
	HANDLE m_hThread;	// Thread handle<br />
	UINT   uiThreadId;	//<br />
<br />
	bool m_bActive; //activity indicator<br />
	DWORD m_lpId; //Thread ID<br />
};<br />






#include "stdafx.h"<br />
#include ".\ClxThread.h"<br />
#include <process.h><br />
<br />
/*********************************************************************************************<br />
Purpose: Constructor <br />
**********************************************************************************************/<br />
ClxThread::ClxThread()<br />
{<br />
	m_hThread = NULL;	<br />
	m_bActive = false;<br />
	uiThreadId = NULL;<br />
}<br />
<br />
/*********************************************************************************************<br />
Purpose: Destructor<br />
**********************************************************************************************/<br />
ClxThread::~ClxThread()<br />
{<br />
	Kill();<br />
}<br />
<br />
bool ClxThread::CreateNewThread(PTHREADFUNC pThreadFunc)<br />
{<br />
	if( m_hThread == NULL )<br />
	{<br />
	<br />
		<br />
	<br />
		// _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID)<br />
		m_hThread = (HANDLE)_beginthreadex(NULL,   0,	pThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);	//<br />
<br />
		if ( NULL != m_hThread)<br />
		{<br />
			//SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);<br />
			ResumeThread( m_hThread );<br />
			m_bActive = true;<br />
			return true;<br />
		}else{<br />
			m_bActive = false;<br />
<br />
		}<br />
	}<br />
<br />
	return true;<br />
}<br />
/*********************************************************************************************<br />
Purpose:	Creates a new thread as long if no other thread is currently running<br />
**********************************************************************************************/<br />
bool ClxThread::CreateNewThread(void)<br />
{<br />
	if( m_hThread == NULL )<br />
	{<br />
		<br />
		<br />
	<br />
		// _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID)<br />
		m_hThread = (HANDLE)_beginthreadex(NULL,   0,	_ThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);	//<br />
<br />
		if ( NULL != m_hThread)<br />
		{<br />
			//SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);<br />
			ResumeThread( m_hThread );<br />
			m_bActive = true;<br />
			return true;<br />
		}else{<br />
			m_bActive = false;<br />
<br />
		}<br />
	}<br />
<br />
	return true;<br />
}<br />
<br />
/*********************************************************************************************<br />
Purpose:	Suspends the Current thread <br />
**********************************************************************************************/<br />
bool ClxThread::Suspend(void)<br />
{<br />
	m_bActive = false;<br />
	return (-1 != SuspendThread(m_hThread));//win32 API<br />
}<br />
<br />
<br />
/*********************************************************************************************<br />
Purpose:  Kills the Current Thread<br />
**********************************************************************************************/<br />
bool ClxThread::Kill(void)<br />
{<br />
	if( TerminateThread(m_hThread, 1) ) //win32 API<br />
	{<br />
		m_hThread = NULL;<br />
		m_bActive  = false;<br />
		return 1;<br />
	}else{<br />
		return 0;<br />
	}<br />
}<br />
<br />
<br />
/*********************************************************************************************<br />
Purpose:  Restarts a suspend thread<br />
**********************************************************************************************/<br />
bool ClxThread::Resume(void)<br />
{<br />
	m_bActive = true;<br />
	return (-1 != ResumeThread(m_hThread)); //win32 API<br />
}<br />
<br />
<br />
/*********************************************************************************************<br />
Purpose:  <br />
**********************************************************************************************/<br />
bool ClxThread::Wait(void)<br />
{<br />
	return (WAIT_OBJECT_0 == WaitForSingleObject(m_hThread, INFINITE));<br />
	//win32 API<br />
}<br />
<br />
<br />
/*********************************************************************************************<br />
Purpose:  Varifies if a thread is active <br />
**********************************************************************************************/<br />
bool ClxThread::IsActive(void)<br />
{<br />
	return m_bActive;<br />
}<br />
<br />
<br />
<br />
<br />
//Actual thread around which the OO wrapper is built.<br />
//Do not call twice on the same object.<br />
//do something (initializing and finalizing) in ThreadEntry and ThreadExit functions.<br />
/**********************************************************************************<br />
Function: MaintainingThreadProcc() <br />
Purpose: This function is the main thread for socket communication - Asynchronous mode.<br />
         This function maintains the socket connection.<br />
Arg 1:  LPVOID pParam, Thread parameter - a ClxUdpSocket pointer<br />
**********************************************************************************/<br />
UINT WINAPI ClxThread::_ThreadFunc(LPVOID pParam)<br />
{<br />
	ClxThread* pThis = reinterpret_cast<ClxThread*>( pParam );<br />
	_ASSERTE( pThis != NULL );<br />
<br />
	pThis->ThreadEntry(); //initialize<br />
	pThis->ThreadRun();<br />
	pThis->ThreadExit(); //finalize<br />
<br />
	pThis->m_bActive = false;<br />
	pThis->m_hThread = NULL;<br />
	pThis->m_lpId = NULL;<br />
<br />
	return 1L;<br />
} // end SocketThreadProc


Scott Dolan
Jernie Corporation
Engineering & Manufacturing
Software, Hardware, & Enclosures

QuestionRe: The purpose of the thread class Pin
Mark Salsbery14-May-07 11:16
Mark Salsbery14-May-07 11:16 
GeneralRe: The purpose of the thread class Pin
led mike14-May-07 12:33
led mike14-May-07 12:33 
GeneralRe: The purpose of the thread class Pin
ScotDolan15-May-07 3:26
ScotDolan15-May-07 3:26 
GeneralRe: The purpose of the thread class Pin
led mike15-May-07 5:43
led mike15-May-07 5:43 
AnswerRe: The purpose of the thread class Pin
ScotDolan15-May-07 6:14
ScotDolan15-May-07 6:14 
GeneralRe: The purpose of the thread class Pin
led mike15-May-07 7:04
led mike15-May-07 7:04 
AnswerRe: C++ Multiple inheritance Pin
led mike14-May-07 8:01
led mike14-May-07 8:01 
QuestionNetwork Machines Pin
rw10414-May-07 4:33
rw10414-May-07 4:33 
AnswerRe: Network Machines Pin
led mike14-May-07 4:47
led mike14-May-07 4:47 
GeneralRe: Network Machines Pin
rw10414-May-07 5:01
rw10414-May-07 5:01 
QuestionRe: Network Machines Pin
David Crow14-May-07 4:53
David Crow14-May-07 4:53 
AnswerRe: Network Machines Pin
rw10414-May-07 5:00
rw10414-May-07 5:00 
QuestionSimple GDI program Pin
Dustin Henry14-May-07 4:28
Dustin Henry14-May-07 4:28 
AnswerRe: Simple GDI program Pin
Arman S.14-May-07 5:40
Arman S.14-May-07 5:40 
GeneralRe: Simple GDI program Pin
Dustin Henry14-May-07 6:20
Dustin Henry14-May-07 6:20 
GeneralRe: Simple GDI program Pin
Arman S.14-May-07 6:57
Arman S.14-May-07 6:57 
AnswerRe: Simple GDI program [modified] Pin
Mark Salsbery14-May-07 5:56
Mark Salsbery14-May-07 5:56 

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.