basethread.h头文件如下:
view plaincopy to clipboardprint?
#ifndef __BASE_THREAD_HEAD_FILE__
#define __BASE_THREAD_HEAD_FILE__
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <iostream>
#include <string>
#include "blmutex.h"
class CBaseThread;
typedef void *(*TaskThread ) ( void * );
struct Task
{
pthread_t _threadID;
std::string _threadName;
CBaseThread *_lpThread;
bool start( TaskThread taskthread, CBaseThread *lpBaseThread, const char *threadName )
{
_lpThread = lpBaseThread;
_threadName = threadName;
pthread_create ( &_threadID, NULL, taskthread, this );
_stop = false;
return true;
}
bool end()
{
{
BL_Lock bl ( &_mutex );
_stop = true;
}
pthread_join (_threadID, NULL );
return true;
};
bool IsStop()
{
bool stop;
{
BL_Lock bl ( &_mutex );
stop = _stop;
}
return stop;
}
Task()
{
_lpThread = NULL;
}
BL_Mutex _mutex;
bool _stop;
};
class CBaseThread
{
public:
CBaseThread(){};
~CBaseThread()
{
end();
};
public:
bool start(const char *threadName )
{
m_task.start(RunThread, this, threadName );
};
bool end()
{
m_task.end();
};
public:
virtual bool Init() = 0;
virtual void Runonce() = 0;
virtual void Clear(){};
public:
static void *RunThread( void *task )
{
Task *lpTask = (Task*)task;
lpTask->_lpThread->Run();
lpTask->end();
return (void*)0;
}
public:
void Run()
{
if ( !Init() )
{
}
while ( m_task.IsStop() )
{
Runonce();
}
Clear();
}
private:
Task m_task;
};
#endif //__BASE_THREAD_HEAD_FILE__
#ifndef __BASE_THREAD_HEAD_FILE__
#define __BASE_THREAD_HEAD_FILE__
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <iostream>
#include <string>
#include "blmutex.h"
class CBaseThread;
typedef void *(*TaskThread ) ( void * );
struct Task
{
pthread_t _threadID;
std::string _threadName;
CBaseThread *_lpThread;
bool start( TaskThread taskthread, CBaseThread *lpBaseThread, const char *threadName )
{
_lpThread = lpBaseThread;
_threadName = threadName;
pthread_create ( &_threadID, NULL, taskthread, this );
_stop = false;
return true;
}
bool end()
{
{
BL_Lock bl ( &_mutex );
_stop = true;
}
pthread_join (_threadID, NULL );
return true;
};
bool IsStop()
{
bool stop;
{
BL_Lock bl ( &_mutex );
stop = _stop;
}
return stop;
}
Task()
{
_lpThread = NULL;
}
BL_Mutex _mutex;
bool _stop;
};
class CBaseThread
{
public:
CBaseThread(){};
~CBaseThread()
{
end();
};
public:
bool start(const char *threadName )
{
m_task.start(RunThread, this, threadName );
};
bool end()
{
m_task.end();
};
public:
virtual bool Init() = 0;
virtual void Runonce() = 0;
virtual void Clear(){};
public:
static void *RunThread( void *task )
{
Task *lpTask = (Task*)task;
lpTask->_lpThread->Run();
lpTask->end();
return (void*)0;
}
public:
void Run()
{
if ( !Init() )
{
}
while ( m_task.IsStop() )
{
Runonce();
}
Clear();
}
private:
Task m_task;
};
#endif //__BASE_THREAD_HEAD_FILE__
