C++简单封装pthread

来源:互联网 发布:穷文富武 知乎 编辑:程序博客网 时间:2024/05/21 08:36
////  CppThread.h//  CppThread////  Created by mrsimple on 4/18/14.//  Copyright (c) 2014 mrsimple. All rights reserved.//#ifndef __CppThread__CppThread__#define __CppThread__CppThread__#include <iostream>#include <pthread.h>#include <string>using namespace std;#define null NULL/** * C++线程类简单封装 * */class AbstractThread{public:    AbstractThread();    //    virtual ~AbstractThread();    // create a new thread and invoke the run method.    void start();    // wait the thread to finish.    void join();        // 获取当前线程id    inline pthread_t& getThreadId() ;    // 获取当前线程属性    inline pthread_attr_t& getThreadAttr() ;    //    // run method will be invoke in the friend method.//    friend void* runAgent(void* arg);protected:    // 线程的执行的回调方法, 在该方法中调调用该类的run函数    static void* runProxy(void* instance) ;    // 由子类来实现, 线程的工作函数    virtual void run() = 0;protected:    pthread_t mThreadId;    pthread_attr_t mThreadAttr ;    string mName;    static unsigned int mIndex;    private:    void setDefaultThreadName();} ;#endif /* defined(__CppThread__CppThread__) */


////  CppThread.cpp//  CppThread////  Created by mrsimple on 4/18/14.//  Copyright (c) 2014 mrsimple. All rights reserved.//#include "CppThread.h"#include <sstream>//void* runAgent(void* arg)//{//    if ( NULL != arg )//    {//        cout<<"My Thread id : "<<pthread_self() <<endl;//        AbstractThread* current = static_cast<AbstractThread*>(arg) ;//        current->run() ;//    }//    return NULL;//}unsigned int AbstractThread::mIndex = 0;// 构造AbstractThread::AbstractThread():mName("Thread - "){    mIndex++;    // 初始化属性    pthread_attr_init( &mThreadAttr ) ;    setDefaultThreadName() ;}// 析构AbstractThread::~AbstractThread(){    pthread_attr_destroy(&mThreadAttr) ;}// 设置默认名字void AbstractThread::setDefaultThreadName(){    stringstream sstream ;    sstream<<mIndex;    mName += sstream.str() ;    cout<<"Default Thread Name : "<<mName<<endl;}// 启动线程void AbstractThread::start(){    // 创建线程, 线程的执行的回调方法为静态函数runProxy函数, 传递this给该函数, 然后runProxy中调调用该类的run函数    int ret = pthread_create(&mThreadId, &mThreadAttr, AbstractThread::runProxy, this) ;    // 方式2, 使用友元函数, runAgent//    int ret = pthread_create(&mThreadId, &mThreadAttr, runAgent, this) ;    if ( ret == 0 )    {        cout<<"success"<<endl;    }    else    {        cout<<"failed"<<endl;    }   }//void AbstractThread::join(){    pthread_join(mThreadId, NULL) ;}//void* AbstractThread::runProxy(void *instance){    if ( instance != NULL )    {        // static_cast不仅可以用在指针和引用上,还可以用在基础数据和对象上, 不进行类型检查        AbstractThread* current = static_cast<AbstractThread*>(instance) ;        current->run() ;    }    return NULL;}//pthread_t& AbstractThread::getThreadId(){    return mThreadId;}// pthread_attr_t& AbstractThread::getThreadAttr(){    return mThreadAttr;}

main : 

////  main.cpp//  CppThread////  Created by mrsimple on 4/18/14.//  Copyright (c) 2014 mrsimple. All rights reserved.//#include <iostream>#include <pthread.h>#include "CppThread.h"using namespace std;// 获取线程idunsigned long getMyThreadId(){    return (unsigned long) pthread_self() ;}// 线程子类, 覆写run方法, 把要做的工作写在其中, 类似于Java中的Runnableclass Thread : virtual public AbstractThread{protected:    void run() {        cout<<"-------------------START------------------"<<endl;        cout<<"sub pthread  -->  tid : "<<getMyThreadId()<<endl;        cout<<"--------------------END-----------------"<<endl<<endl;    }} ;//int main(int argc, const char * argv[]){    cout<<"Main Thread id : "<<pthread_self()<<endl;        Thread th;    Thread th2 ;    th.start();    th2.start();        th.join();    th2.join();        return 0;}

结果 : 

0 0
原创粉丝点击