实现线程的封装, 2种方式(面向对象,基于对象)
2017-03-15 13:26
609 查看
//实现线程的封装(面向对象)
//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;
}
//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;
}
相关文章推荐
- JAVA线程的应用实例(运用2种中断线程方式,基于实现进度条为例)
- JAVA线程的应用实例(运用2种中断线程方式,基于实现进度条为例)
- JAVA线程的应用实例(运用2种中断线程方式,基于实现进度条为例)
- javascript面向对象中的对象创建、继承、封装等实现方式
- 实际项目中以java面向对象的方式实现K-means算法,把对象聚类
- C语言基于对象编程实现封装
- [Andriod]计时器实现-基于线程消息机制三种方式
- Stub方式接口化封装axis2 webservice:实现衍生对象与原始对象的相互转换
- 基于boost 线程并行技术实现消息队列方式[记录]
- 优雅设计封装基于Okhttp3的网络框架(三):多线程下载功能核心实现 及 线程池、队列机制、终止线程解析
- Python使用面向对象方式创建线程实现12306售票系统
- 数据列表实现方式(基于Vue.js封装)
- python 以面向对象的方式创建线程 实现售票系统
- JavaScript 面向对象(封装、继承、多态)多种方式实现完全总结
- C++实现线程同步的几种方式 线程同步是指同一进程中的多个线程互相协调工作从而达到一致性。之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏,下面是多
- JavaScript面向对象-基于原型链和函数伪装组合的方式实现继承
- [Python]基于权重的随机数2种实现方式
- Linux学习之线程封装三:基于模板的面向对象的封装
- 线程的2种实现方式
- 对象(类)的封装方式(面向对象的js基本知识)