实现线程的封装, 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()));
thread.join();
return 0;
}
- 实现线程的封装, 2种方式(面向对象,基于对象)
- C++中面向对象和基于对象的线程封装方法
- 面向对象风格的线程类封装
- linux开发:线程的面向对象封装
- JavaScript面向对象-基于函数伪造的方式实现继承
- 基于对象风格的线程类封装
- 面向对象的封装
- 面向对象的封装
- 面向对象的线程池Threadpool的封装
- 基于面向对象封装的模态对话框类
- Lua的面向对象实现方式
- 用面向对象的方式封装javascript代码
- lua面向对象的2种私密性封装
- linux C++ 面向对象线程类封装
- linux C++ 面向对象线程类封装
- linux C++ 面向对象线程类封装
- C#实现面向对象多态性的三种方式
- Tkinter -- 面向对象方式简单封装
- final
- 程序中注册dll
- L1-005 考试座位号 java
- CRSA应急频率表
- lsof使用
- 实现线程的封装, 2种方式(面向对象,基于对象)
- C#疑问知识
- win7 wamp环境安装php-redis扩展
- node.js npm 模块设置报错FATAL ERROR: MarkCompactCollector:
- 抽象类abstract
- 商业智能在零售业中的应用
- Java垃圾回收机制知识点总结
- 选中点击项和样式的移入移除
- 安装mysql提示This application requires .NET framework 4.0