linux 下 线程封装

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<pthread.h>
#include<queue>
#include<string>
#include<string.h>


using namespace std;

/*************************************
 *author:郑金玮
 *time:2014/07/16
 *desc:implement thread package in cpp class at linux
 ********************************** */
typedef unsigned int  uint32;
typedef int           int32;
typedef int64_t       uint64;

typedef short         int16;
typedef unsigned short uint16;
typedef unsigned long bc_dword;
typedef unsigned short bc_word;
typedef unsigned char bc_byte;
typedef size_t   bc_size;
typedef void*     bc_HANDLE;


#define DISALLOW_CPY_ASSIGN(type) private: \
        type(const type&); \
        type& operator = (const type&);


class CThreadLinux
{
public:
        CThreadLinux();
        virtual  ~CThreadLinux();

        int GetHandle() const;
        void Start();
        bc_dword Terminate(bc_dword exitCode = 0);

public:
        virtual int Run() =0;
        static void* ThreadFunction(void *PV);
        void ResetHandle();

        DISALLOW_CPY_ASSIGN(CThreadLinux);
private:
        int32 m_hThread;
};


CThreadLinux::CThreadLinux()
{
        m_hThread = 0;
}
CThreadLinux::~CThreadLinux()
{
        ResetHandle();
}


int  CThreadLinux::GetHandle() const
{
        return m_hThread;
}
        

void  CThreadLinux::Start()
{
        if(m_hThread == 0)
        {
                pthread_t __ntid;
                m_hThread = pthread_create(&__ntid,0,ThreadFunction,(void*)this);
        }
}
        
bc_dword  CThreadLinux::Terminate(bc_dword exitCode)
{
        if(m_hThread != 0)
        {
                pthread_exit(NULL);
                return 0;
        }
        return 0;
}

      
void*  CThreadLinux::ThreadFunction(void *PV)
{
        CThreadLinux *pThis = (CThreadLinux*)PV;
        int32 __ret = pThis->Run();

        pthread_detach(pthread_self());// set thread mode to nonblock mode and return now
        pThis->ResetHandle();
        return NULL;
}
     
void  CThreadLinux::ResetHandle()
{
        if(m_hThread != NULL)
        {
                m_hThread = 0;
        }
}

/this is a demo for test thread function//

class CLog : public CThreadLinux
{

private:
        CLog(){}
        ~CLog(){}
public:
        static CLog* instance();
        void log(string strlog)
        {
                m_msgQueue.push(strlog);
        }

        int Run()
        {
                while(true)
                {
                        if(!m_msgQueue.empty())
                        {
                                cout<<m_msgQueue.front()<<"\n"<<endl;
                                fflush(stdout);
                                m_msgQueue.pop();
                        }
                }
                return 0;
        }
private:
        queue<string> m_msgQueue;
};


CLog* CLog::instance()
{
        static CLog __instance;
        return &__instance;
} 

#define LOG() CLog::instance()


int main()
{
        int32 num = 1000;
        int32 i=1;
        char szLogs[256];

        LOG()->Start();

        while( i <= num )
        {
                memset(szLogs,0,256);
                sprintf(szLogs,"log index %d",i);
                LOG()->log(szLogs);
                i++;
        }

        for(;;);

        return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值