线程封装类(1)

来源:互联网 发布:如何判定论文数据造假 编辑:程序博客网 时间:2024/06/04 19:57
#ifndef __THREAD_H__
#define __THREAD_H__

#ifndef _WIN32
#include <pthread.h>
#include <errno.h>
#endif



#define PRIORITY_HIGHEST_OUR THREAD_PRIORITY_HIGHEST
#define PRIORITY_HIGH_OUR THREAD_PRIORITY_ABOVE_NORMAL
#define PRIORITY_NORMAL_OUR THREAD_PRIORITY_NORMAL
#define PRIORITY_LOW_OUR THREAD_PRIORITY_BELOW_NORMAL
#define PRIORITY_LOWEST_OUR THREAD_PRIORITY_LOWEST


class  WThread{

public:
WThread( );
virtual ~WThread();

public:

virtual BOOL StartThread( BOOL bWaitCreated = TRUE,void* pThreadContext = NULL );

virtual void SetAutoDelete( BOOL bAutoDelete )  { m_bAutoDelete = bAutoDelete; };

virtual DWORD Suspend(void);

virtual DWORD Resume(void);

virtual BOOL SetPriority(int nPriority);

virtual BOOL GetExitCode(DWORD *pdwExitCode);

virtual void StopThread(void);

virtual DWORD ThreadProcEx(){return 0;};

virtual DWORD   GetThreadID( );

virtual BOOL IsStarted();
public:
BOOL m_bAutoDelete;
DWORD m_dwExitCode;
#ifdef _WIN32
WTHREADID m_unThreadID;
HANDLE m_hThread;
HANDLE m_evCreated;
#else
pthread_t m_thrd;
#endif
};
}

#endif


.cpp    


#include "StdAfx.h"

#ifdef _WIN32
#ifndef _WIN32_WCE
#include <process.h>
#endif
#endif


#include "assert.h"
#include "WThread.h"


#ifdef _WIN32


unsigned __stdcall InitThreadProc(PVOID pObj)
{
WThread *pThread = (WThread *)pObj;
if( pThread->m_evCreated ){
//Create Message Queue
MSG msg;
PeekMessage( &msg,NULL,WM_USER, WM_USER, PM_NOREMOVE);
SetEvent( pThread->m_evCreated );
}
DWORD dwRet = pThread->ThreadProcEx();
pThread->m_dwExitCode = dwRet;
if( pThread->m_bAutoDelete )
delete pThread;
return dwRet;
}


WThread::WThread( )
:m_unThreadID(0),
 m_dwExitCode( 0 ),
 m_hThread( NULL ),
 m_bAutoDelete( FALSE ),
 m_evCreated( NULL )
{
}


WThread::~WThread()
{
}


BOOL WThread::StartThread( BOOL bWaitCreated,void* pThreadContext )
{
if (!m_hThread){ 
if( bWaitCreated ){
m_evCreated = CreateEvent( NULL,TRUE,FALSE,NULL );
}
#ifdef _WIN32_WCE
m_hThread = (HANDLE)CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE )InitThreadProc,
pThreadContext?pThreadContext:(LPVOID)this,
0,
&m_unThreadID
);
#else
m_hThread = (HANDLE)_beginthreadex(
NULL,
0,
InitThreadProc,
pThreadContext?pThreadContext:(LPVOID)this,
0,
&m_unThreadID
);
#endif
if( bWaitCreated ){
DWORD dwRet = WaitForSingleObject( m_evCreated,15000 );
assert( dwRet == WAIT_OBJECT_0 );
CloseHandle( m_evCreated );
m_evCreated = NULL;
}
}
return m_hThread != NULL;
}


void WThread::StopThread(void)
{
if (m_hThread){

WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread);
InterlockedExchange((LONG *)&m_hThread, 0);
}


m_hThread=0;
m_unThreadID=0;
}


DWORD WThread::Suspend(void)
{
return SuspendThread(m_hThread);
}

DWORD WThread::Resume(void)
{
return ResumeThread(m_hThread);
}

BOOL WThread::SetPriority(int nPriority)
{
return SetThreadPriority(m_hThread,nPriority);
}

BOOL WThread::GetExitCode(DWORD *pdwExitCode)
{
return ::GetExitCodeThread(m_hThread,pdwExitCode);
}


BOOL WThread::IsStarted()

return m_hThread != NULL;
};


DWORD   WThread::GetThreadID( )
{
return m_unThreadID;
};


#else


extern "C"
{
static void* InitThreadProc( void* arg )
{
WThread *pThread = (WThread *)arg;


DWORD dwRet = pThread->ThreadProcEx();
pThread->m_dwExitCode = dwRet;


if( pThread->m_bAutoDelete )
delete pThread;
return 0;
}
}


WThread::WThread( ):
 m_bAutoDelete( FALSE ),
 m_dwExitCode(0),
 m_thrd( 0 )
{
}


WThread::~WThread()
{
}


BOOL WThread::StartThread( BOOL bWaitCreated,void* pThreadContext )
{
if (!m_thrd ){ 
int nRet = pthread_create( &m_thrd,0,InitThreadProc,this );
if( nRet != 0 )
return FALSE;
}
return m_thrd != 0;
}


void WThread::StopThread(void)
{
if ( m_thrd ){

void *ignore = 0;
pthread_join( m_thrd,&ignore );
pthread_detach( m_thrd );
}


m_thrd=0;
}


DWORD WThread::Suspend(void)
{
#ifdef _WIN32
return SuspendThread(m_hThread);
#else
return 0;
#endif
}

DWORD WThread::Resume(void)
{
#ifdef _WIN32
return ResumeThread(m_hThread);
#else
return 0;
#endif
}

BOOL WThread::SetPriority(int nPriority)
{
#ifdef _WIN32
return SetThreadPriority(m_hThread,nPriority);
#else
return FALSE;
#endif
}

BOOL WThread::GetExitCode(DWORD *pdwExitCode)
{
if( pdwExitCode )
*pdwExitCode = m_dwExitCode;
return TRUE;
}

BOOL WThread::IsStarted()

return m_thrd != 0;
};


DWORD   WThread::GetThreadID( )
{
return (DWORD)m_thrd;
};


#endif





2 0
原创粉丝点击