Archived

This topic is now archived and is closed to further replies.

jermz

win32 Thread class

Recommended Posts

I was just trying to make a generic thread class that I could use to derive other classes. Here is what I came up with:
  
#ifndef		WWSTPTHREAD_H
#define		WWSTPTHREAD_H

#ifdef		WIN32


#include	<windows.h>
#include	<process.h>

#include	"WWSTPLock.h"


#define		WWSTPTHREAD_TIMEOUT		5000


class WWSTPThread
{

private:

	HANDLE		m_hThread;		// handle to the thread

	WWSTPLock	m_lock;			// lock mechanism used for safe stopping

	bool		m_bActive;		// boolean whether thread is running


public:

private:

	static unsigned int _stdcall threadFunc(void* threadObj)
	{
		(reinterpret_cast <WWSTPThread *>(threadObj))->run();

		::_endthreadex(0);
		return 0;
	}

public:

	WWSTPThread()
	{
		m_hThread = NULL;
		m_bActive = false;
	}

	virtual ~WWSTPThread()
	{

	}
 
	/////////////////////////////////////////////////////////////////////

	// Starts the thread running so that it enters the virtual

	// run() method.

	/////////////////////////////////////////////////////////////////////

	void start()
	{
		m_lock.lock();

			if (!m_bActive)
			{
				m_hThread = (HANDLE) ::_beginthreadex(NULL, 0, threadFunc, this, 0, NULL);

				if (m_hThread)
					m_bActive = true;
			}

		m_lock.unlock();
	}

	/////////////////////////////////////////////////////////////////////

	// Asks the thread to stop and waits until a timeout period before

	// killing the thread if necessary. Closes thread handle.

	/////////////////////////////////////////////////////////////////////

	void stop()
	{
		m_lock.lock();

			if (m_bActive)
			{
				// ask thread to stop via virutal end() method

				this->end();

				// wait until thread is signaled

				DWORD retval = ::WaitForSingleObject(m_hThread, WWSTPTHREAD_TIMEOUT);

				// if we''ve waited our timeout for this thread to stop, kill it forcefully

				if (retval == WAIT_TIMEOUT)
					::TerminateThread(m_hThread, 1);

				// clean up our resources

				CloseHandle(m_hThread);
				m_bActive = false;
			}

		m_lock.unlock();
	}

	/////////////////////////////////////////////////////////////////////

	// These are virtual methods that implement the specific running of

	// and a way to end the threads in question.

	/////////////////////////////////////////////////////////////////////

	virtual void run()=0;
	virtual void end()=0;

};


#endif	// WIN32


#endif	// WWSTPTHREAD_H

  
The derived classes would implement run(), which is the heart of the thread; end(), which politely asked the thread to stop and exit sometime in the future. WWSTPLock is just a critical section wrapper. Is there anything that appears to be obviously wrong or troublesome? Theres no way to pause it yet...and the derived class is responsible for stopping the thread if its still running in the destructor (via stop()). Its been a while since I used c++. It seems to work, but then again the problems with multi-threading are those that happen infrequently.

Share this post


Link to post
Share on other sites