Click here to Skip to main content
13,299,727 members (59,180 online)
Click here to Skip to main content

Stats

30.9K views
498 downloads
13 bookmarked
Posted 15 Oct 2006

Cross Platform Threads

, 15 Oct 2006
A cross platform thread class.
/*******************************************************************************
** FILE:			XPThreads.h

** DESCRIPTION: 

** AUTHOR:			Amit Gupta

** CREATED:		
********************************************************************************/

#include "XPThreads.h"


/******************************************************************************
** FUNCTION:	Constructor 
*******************************************************************************/
XPThreads::XPThreads(ThreadProc ptrCallback, long nTimeout)
: m_Callback(ptrCallback)
, m_Threadid(0)
#if _WINDOWS
, m_nTimeout(nTimeout)
, m_ptrThread(NULL)
#endif
{
}

/******************************************************************************
** FUNCTION:	Destructor 
*******************************************************************************/
XPThreads::~XPThreads() throw()
{
	Stop();	
}


/******************************************************************************
** FUNCTION:	Stop 
*******************************************************************************/
void XPThreads::Stop()
{
#if _WINDOWS
	//waiting for the thread to terminate
	if (m_ptrThread) 
	{
		if(WAIT_TIMEOUT == ::WaitForSingleObject (m_ptrThread, m_nTimeout))
			::TerminateThread (m_ptrThread, 1);

		::CloseHandle (m_ptrThread);
	}
#endif
}

/******************************************************************************
** FUNCTION:	Run 
*******************************************************************************/
bool XPThreads::Run()
{
	bool bRetVal = false;
	
	do
	{		
		if(IsThreadCreated())
			break;

#if _WINDOWS

		m_ptrThread = ::CreateThread (NULL, 0, 
			(unsigned long (__stdcall *)(void *))m_Callback, 
			NULL, 0, &m_Threadid);
		if(NULL == m_ptrThread)
			break;
	
#elif __MACH__
	    pthread_attr_t attr;
	
		/* Initialize and set thread detached attribute */
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);				
	
		if(!pthread_create(&m_Threadid, &attr, m_Callback, (void *)this))
			break;

	   /* Free attribute and wait  */
		pthread_attr_destroy(&attr);
#endif

		bRetVal = true;
	}while(false);
	
	return bRetVal;
}

/******************************************************************************
** FUNCTION:	SetThreadProc
*******************************************************************************/
void XPThreads::SetThreadProc(ThreadProc ptrCallback)
{
	m_Callback = ptrCallback;
}

/******************************************************************************
** FUNCTION:	IsThreadCreated
*******************************************************************************/
bool XPThreads::IsThreadCreated()
{
	return m_Threadid ? true : false;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amit Gupta MDS
Product Manager MetaDesign Solutions
India India
Senior Software Developer with special skills in Developing Applications for Macintosh and Windows.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 16 Oct 2006
Article Copyright 2006 by Amit Gupta MDS
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid