本人刚来没多久,没用过博客,到现在还是觉得晕,为什么别人随笔里内容什么都有,不会发布到外面,我一写就发布出去了,不会用啊,惭愧,知道的告我吧。
这是一个封装的线程类,其中的一些对象到我博客找吧。
///////////////////////////////////////////H//////////////////////////////////////////////////
#ifndef LGLIB_THREAD_H
#define LGLIB_THREAD_H
#if _MSC_VER > 1000
#pragma once
#endif
#include <windows.h>
#include "Lg_SyncLock.h"
namespace lglib
{
/*!
* 在一个应用程序中执行的每一个线程是通过初始化一个派生自lg_Thread
* 类的一个实例来创建的。这些派生类必须实现run()方法,在这里执行相应
* 的代码。
*/
class __LGLIB__ lg_Thread
{
public:
lg_Thread(uint32 stack = 0,LPCTSTR szName = NULL,int32 nPriority =THREAD_PRIORITY_NORMAL);
virtual ~lg_Thread();
BOOL isRunning();
virtual void start(lg_Semaphore *psem = NULL);
protected:
//用户初始化通知函数
virtual void init(){}
//用户线程数据处理函数
virtual void run() = 0;
//用户程序段出现异常,通知用户处理
virtual void Exception(){}
//退出通知函数
virtual void final();
//通知父线程本线程退出
virtual void notify(lg_Thread* ){}
//线程派生对象一般在析构的时候调用此函数结束线程
void quit();
void terminate(void);
BOOL isCanLoop();
void setExit(){m_evnExit.set();}
void suspend();
void resume();
cpstr getThreadName() const ;
void setThreadName( LPCTSTR szName = NULL );
private:
void close();
static uint32 __stdcall Execute( lg_Thread *pth );
private:
//父线程指针
lg_Thread* m_pthParent;
//线程句柄
HANDLE m_hThread;
//线程优先级
int32 m_nPriority;
//线程ID
uint32 m_tid;
//堆栈大小
uint32 m_nStack;
//线程是否挂起
BOOL m_bActive;
BOOL m_bIsSuspend;
//线程别名
char m_szName[32];
//线程退出事件
lg_Event m_evnExit;
lg_Semaphore* m_psemStart;
};
}
#endif //LGLIB_THREAD_H
////////////////////////////////////////////////CPP////////////////////////////////////////////////////
#include "StdAfx.h"
#include "assert.h"
#include "Lg_Tread.h"
#include "lg_Exception.h"
#include <iostream>
using namespace std;
namespace lglib
{
lg_Thread::lg_Thread(uint32 stack,LPCTSTR szName,int32 nPriority)
: m_hThread(INVALID_HANDLE_VALUE),m_bActive(FALSE),m_bIsSuspend(FALSE),
m_tid(0),m_nStack(stack),m_pthParent(NULL),m_nPriority(nPriority),m_psemStart( NULL )
{
setThreadName(szName);
m_evnExit.reset();
}
lg_Thread::~lg_Thread()
{
LGTRACE(Output_Console,"%s thread End in",m_szName);
m_evnExit.set();
//join();
terminate();
LGTRACE(Output_Console,"%s thread End out",m_szName);
}
void lg_Thread::start( lg_Semaphore *psem)
{
if(m_bActive)
throw lg_Exception("lg_Thread::start","线程已经运行",LG_THREAD_HAVE_RUN);
m_hThread = (HANDLE)CreateThread( NULL, m_nStack, ( exec_t )&Execute, this,
CREATE_SUSPENDED, &m_tid);
if ( !m_hThread ) {
throw( lg_Exception( "lg_Thread::start::CreateThread",
"创建线程对象失败",
LG_THREAD_NO_OBJECT ) );
}
m_bIsSuspend = FALSE;
m_psemStart = psem;
::SetThreadPriority( m_hThread, m_nPriority );
ResumeThread( m_hThread );
LGTRACE(Output_Console,"%s THREAD STARTED",m_szName);
m_bActive = TRUE;
}
void lg_Thread::quit()
{
if(m_bIsSuspend)
resume();
setExit();
if( m_hThread)
{
WaitForSingleObject( m_hThread, INFINITE );
::CloseHandle( m_hThread);
m_hThread = NULL;
}
}
void lg_Thread::close()
{
if( m_pthParent )
{
m_pthParent->notify( this );
}
final();
if(m_bActive)
m_bActive = FALSE;
}
void lg_Thread::terminate(void)
{
cout<<"terminate"<<endl;
bool terminated = false;
if( !m_bActive && m_hThread != NULL )
{
ResumeThread( m_hThread );
TerminateThread( m_hThread, 0 );
terminated = true;
}
if( m_hThread != NULL )
{
WaitForSingleObject( m_hThread, INFINITE );
CloseHandle( m_hThread );
m_hThread = NULL;
}
if( m_pthParent)
{
m_pthParent->notify( this );
}
m_tid = 0;
if ( terminated )
{
final();
}
cout<<"out terminate"<<endl;
}
//check the status of the thread
BOOL lg_Thread::isRunning()
{
return ( m_tid != 0 && m_bActive ) ? TRUE : FALSE;
}
BOOL lg_Thread::isCanLoop()
{
return (m_evnExit.wait( 0 ) != lg_Event::EVENT);
}
//suspend the thread
void lg_Thread::suspend()
{
if( ! m_bActive )
throw( lg_Exception( "lg_Thread::suspend",
"线程没有运行",
LG_THREAD_NO_RUN ) );
if ( ! m_hThread ){
throw(lg_Exception( "lg_Thread::suspend",
"线程句柄无效",
LG_THREAD_NO_HANDLE ) );
}
cout<<"suspend"<<endl;
SuspendThread( m_hThread );
m_bIsSuspend = TRUE;
}
//resume thread
void lg_Thread::resume()
{
if(!m_bActive)
throw( lg_Exception( "lg_Thread::resume",
"线程没有运行",
LG_THREAD_NO_RUN ) );
if ( ! m_hThread ){
throw(lg_Exception( "lg_Thread::suspend",
"线程句柄无效",
LG_THREAD_NO_HANDLE ) );
}
while ( (int) ResumeThread( m_hThread ) > 1 ){cout<<"resume"<<endl;}
m_bIsSuspend = FALSE;
cout<<"resume"<<endl;
}
cpstr lg_Thread::getThreadName() const
{
return m_szName;
}
void lg_Thread::setThreadName( LPCTSTR szName )
{
if(szName){
strcpy(m_szName,szName);
}else{
memset(m_szName,0,sizeof(m_szName));
}
}
void lg_Thread::final()
{
LGTRACE(Output_Console,"%s Thread final",m_szName);
}
//thead process
DWORD __stdcall lg_Thread::Execute( lg_Thread *pth )
{
assert( pth );
if( pth->m_psemStart )
{
pth->m_psemStart->wait();
pth->m_psemStart = NULL;
}
LGTRY
pth->init();
pth->run();
OWCATCH
pth->Exception();
PBCATCH
//忽略异常
LGTRACE(Output_Console,"%s Exception",pth->m_szName);
pth->Exception();
ENDCATCH
pth->final();
LGTRACE(Output_Console,"%s Thread out",pth->m_szName);
return 0;
}
}