博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码回顾
阅读量:4360 次
发布时间:2019-06-07

本文共 3062 字,大约阅读时间需要 10 分钟。

 

1.继承和多态,多线程

做棋牌demo的时候,基本模型都是事件轮询,每个事件一个线程。

所以做了一个基类和派生类。使用了常见的功能。

有几个基础知识点

1.派生类访问基类public方法:基类::方法名字  ,如。IEventLoop::Close();

2.派生类的构造函数不指定,会调用默认的基类构造函数。也可以指定,如 Loop_Log():IEventLoop(){}

3.派生类的构造顺序,基类默认构造,派生类构造。   而派生类析构,是相反,派生类析构,基类析构。

4.管理类mgr 中的成员变量,vecotr<t>,析构的时候会调用元素的析构函数,如果是原始指针类型,那么只是free 掉指针而已。如果是对象,那么必定会调用元素的析构函数。

所以感觉  class EventsMgr的数据成员用vector<shared_ptr<t>>,会更好,这样使用者就不用考虑析构的问题,让使用者能更自然使用new.到堆中。而且如果是原始指针,管理类,无法判断传入者,何时删除它,所以必须用智能指针。

 5.如果要制定顺序关闭服务器。只需要更改下EventsMgr的close 函数。逐个判断是否关闭。 修改后vector元素为共享指针。

 2.环形缓冲区。如果是单线程读,单线称写。就无需锁了。

 

#include 
#include "stdio.h"#include
#include
#include
#include
using namespace std;class IEventLoop{public: IEventLoop():shouldloop(true),stop(false){} virtual void Start()=0; bool IsStart() { return shouldloop; } virtual void Close() { shouldloop=false; } bool IsClosed() { return stop; } virtual ~IEventLoop(){}protected: bool shouldloop; bool stop;};class Loop_Log:public IEventLoop{public: Loop_Log():IEventLoop(){} void Start() { while(shouldloop) { cout<<"log is runing"<
_event) { if(_event!=0) { events.push_back(_event); } } void Start() { for(size_t i=0;i
pi){ pi->Start();}; thread logthread=thread(lamfun,events[i]); logthread.detach(); } } void Close()const { bool allexist=false; for(size_t i=0; i
Close();} } while(!allexist) { allexist=true; for(size_t i=0; i
IsClosed()==false) { allexist=false; break; } } } }private: vector
> events;};//工厂方法.创建对象的时机延迟到工厂类.如果要替换某个对象.只需要到工厂类处理.class LoopFactory{public: static shared_ptr
CreateLog() { return shared_ptr
(new Loop_Log()); } static shared_ptr
CreateServer() { return shared_ptr
(new Epollserver()); }};void CmdStdIn(const EventsMgr& pmgr);int main(){ shared_ptr
pmymgr=shared_ptr
(new EventsMgr); pmymgr->AddEvent(LoopFactory::CreateLog()); pmymgr->AddEvent(LoopFactory::CreateServer()); pmymgr->Start(); CmdStdIn(*pmymgr);}void CmdStdIn(const EventsMgr& pmgr){ string cmd; cin>>cmd; while(cin>>cmd) { if("88"==cmd) { pmgr.Close(); break; } else { } } }

 

 

class RingBuff{public:    RingBuff(int32_t _size);    int32_t Wirte(const char* _souce,int32_t _size);    int32_t Read(char* _desc,int32_t _size);    int32_t Capcity();    int32_t Envalible();    ~RingBuff();    char* value_;private:    int32_t size_;    int32_t p_write;    int32_t p_read;    RingBuff(const RingBuff&);    RingBuff& operator=(const RingBuff&);};RingBuff::RingBuff(int32_t _size):p_write(0),p_read(0),size_(_size){    value_=new char[_size];}int32_t RingBuff::Wirte(const char* _source,int32_t _size){    //最大可写值。是否超过末尾。    int real_size=_size>Envalible()?Envalible():_size;    if(size_-p_write
(size_-Envalible())?size_-Envalible():_size; if(size_-p_read
=p_read) { enLEN=p_read+(size_-p_write); } return enLEN;}RingBuff::~RingBuff(){ delete[] value_; }

 

转载于:https://www.cnblogs.com/lsfv/p/6678666.html

你可能感兴趣的文章
mysql远程链接
查看>>
nginx location配置
查看>>
Easy Install详细参数
查看>>
选课系统
查看>>
最简实例演示asp.net5中用户认证和授权(2)
查看>>
ubuntu rhythmbox乱码解决方法
查看>>
LeetCode题解之Univalued Binary Tree
查看>>
线程池学习研究-(自实现)2
查看>>
ubuntu下安装新字体
查看>>
Django连接MySQL数据库
查看>>
漫游Kafka入门篇之简单介绍(1)
查看>>
redis学习之旅-初识Redis
查看>>
WinForm 小程序 NotePad
查看>>
JSTL 核心标签库 使用
查看>>
线程池ThreadPool
查看>>
hibernate入门实例
查看>>
WPF路由事件二:路由事件的三种策略(转)
查看>>
Java中的内存泄露
查看>>
asp.net 自定义控件验证FCKeditor是否为空
查看>>
oracle 查看表空间的脚本
查看>>