Click here to Skip to main content
11,639,554 members (64,588 online)
Click here to Skip to main content
Add your own
alternative version

Cross Platform Threads

, 15 Oct 2006 CPOL 24.6K 429 13
A cross platform thread class.
XPThreadsDemo.zip
XPThreads
XPThreads.dsp
XPThreads.mcp
XPThreadsSrc.zip
/*******************************************************************************
** 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...

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