实现线程的封装, 2种方式(面向对象,基于对象)

来源:互联网 发布:手机户型平面图软件 编辑:程序博客网 时间:2024/05/16 12:00

//实现线程的封装(面向对象)

//Noncopyable.h

#ifndef __WD_NONCOPYABLE_H__
#define __WD_NONCOPYABLE_H__
namespace wd
{
    class Noncopyable
    {
    protected:
        Noncopyable() {}
        ~Noncopyable(){}
    private:
        Noncopyable(const Noncopyable &);
        Noncopyable & operator=(const Noncopyable &d)   
    };
}//end of namespace wd

#endif


//Thread.h

#include "Noncopyable.h"
#include <pthread.h>
namespace wd
{
class Thread
: private Noncopyable//实现继承
//: public Noncopyable//接口继承
{
public:
    Thread();
    virtural ~Thread();
    virtural void run()=0;//纯虚函数接口
    void start();
    void join();
    static void * threadFunc(void * arg);
private:
    pthread_t _pthId;
    bool _isRunning;
} ;
}//end of namespace wd


//Thread.cc

#include "Thread.h"
#include <iostream>
using std::cout;
using std::endl;
namespace wd
{
Thread::Thread()
: _pthId(0)
, _isRuning(false)
{}
void Thread::start()
{
    pthread_create(&_pthId,NULL,threadFunc,this);
    _isRuning=true;
}
void Thread::join()
{
    if(_isRuning)
    {
        pthread_join(_pthId,NULL);
        _isRuning=false;
    }

}
void * Thread::threadFunc(void * arg)//静态成员函数
{
    Thread * p =static_cast<Thread*>(arg);
    if(p)
        p->run();
    return NULL; 
}
Thread::~Thread()
{
    if(_isRuning)
    {
        pthread_detach(_pthId)
    }
}
}//end of namespace wd


//testThread.cc

#include "Thread.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr;
class DerivedThread:public wd::Thread
{
public:
    virtual void run()
    [
        int cnt=20;
        srand(time(NULL));
        while(cnt--)
        {
            int number=rand()%100;
            cout<<"number="<<number<<endl;
            sleep(1);
        }
    ]
};
int main(void)
{
    unique_ptr<wd::Thread> p(new DerivedThread);//自动回收
    p->start();
    p->join();
    return 0;
}


//实现线程的封装(基于对象)

//Noncopyable.h

#ifndef __WD_NONCOPYABLE_H__
#define __WD_NONCOPYABLE_H__
namespace wd
{

class Noncopyable
{
protected:
Noncopyable(){}
~Noncopyable(){}
private:
Noncopyable(const Noncopyable &);
Noncopyable & operator=(const Noncopyable &);
};
}//end of namespace wd
#endif


//Thread.h

#include "Noncopyable.h"
#include <pthread.h>
#include <functional>
using std::function;
namespace wd
{
class Thread
: private Noncopyable// 实现继承
//: public Noncopyable//接口继承
{
typedef function<void()> ThreadCallback;
public:
Thread(ThreadCallback cb);
~Thread();
void start();
void join();
static void * threadFunc(void * arg);
private:
pthread_t _pthId;
bool _isRunning;
ThreadCallback _cb;//回调函数
};
}//end of namespace wd


//Thread.cc

#include "Thread.h"
namespace wd
{
Thread::Thread(ThreadCallback cb)
:_pthId(0)
,_isRunning(false)
,_cb(cb)
{}
void Thread::start()
{
pthread_create(&_pthId, NULL, threadFunc, this);
_isRunning = true;
}
void * Thread::threadFunc(void * arg)//静态成员函数
{
Thread * p = static_cast<Thread*>(arg);
if(p)
p->_cb();
return NULL;
}
void Thread::join()
{
if(_isRunning)
{
pthread_join(_pthId, NULL);
_isRunning = false;
}
}
Thread::~Thread()
{
if(_isRunning)
{
pthread_detach(_pthId);//将资源回收交给主线程进行托管
_isRunning = false;
}
}
}//end of namespace wd


//testThread.cc

#include "Thread.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using std::cout;
using std::endl;
class Producer
{
public:
void func()//传一个函数对象
{
int cnt = 20;
::srand(::time(NULL));//设置随机数种子
while(cnt--)
{
int number = ::rand() % 100;
cout << "number = " << number << endl;
::sleep(1);//命名空间
}
}
};
int main(void)
{
wd::Thread thread(std::bind(&Producer::func, Producer()));

//通过bind直接传递,Producer()产生命名对象,复制
thread.start();
thread.join();
return 0;
}

0 0
原创粉丝点击