你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下的线程类封装

[日期:2009-12-11] 来源:blog.csdn.net/fo1_sky  作者:fo1_sky [字体:大 中 小]

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__

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款