C++并发编程框架Theron(7)——Theron中包含的类(一)

1 前言
  前面的文章我先后介绍了Theron框架的理论,然后又介绍了Theron框架的实践与几个实例。当然,官方网站中还有很多值得学习的小案例的介绍,此外如果你想自己调试更多小程序开源库中也有对应的工程,我也不再过多的讲解。从本篇博文开始,我会简要说明一下Theron框架中包含的类,函数等。
2 Theron::Actor类
 2.1 简介
  Actor是actor的基础类,Theron中所有的actors的构建都必须继承自该类。它提供actor的核心功能,比如注册消息处理函数和发送消息来回应收到来自其他actors的消息。当完成从该基础类派生,我们可以调用基础类中各种protected方法。
 尽管继承的actors类可以直接在用户代码中被构造,但是他们还是需要与一个Framework绑定,从而来管理与执行它们。这个Framework同时在构造的时候提供给Actor基础类。,形式如下:

class MyActor : public Theron::Actor
{
public:
    MyActor(Theron::Framework &framework) : Theron::Actor(framework)
    {
    }
};

 一个Actor模型的基本原则是actors应该仅可以通过消息通信。因为Theron中的Actors还是以C++类的形式继承自Actor基础类,你在实际中也可能增加传统成员方程来继承actor类,从而打破Actor模型的抽象化。但是这种行为应该被避免,因为它会造成代码非常“ugly”,并且存在共享内存的问题,从而再引入传统线程同步的问题。我们要忍受住为一个actor类增加一个方法,尽可能通过增加一个消息来替代。
 2.2 公共的成员函数

// 显示构造函数
Actor (Framework &framework, const char *const name=0)
// 基础类虚析构函数
virtual ~Actor ()
// 返回actor的地址(地址第一无二)
Address GetAddress () const
// 返回一个管理该actor的framework引用
Framework & GetFramework () const
// 获取目前这个actor消息队列中等待处理的消息总数
uint32_t GetNumQueuedMessages () const

 2.3 保护的成员函数

// 为一个具体的消息类型注册一个处理函数
template<class ActorType , class ValueType >
bool RegisterHandler (ActorType *const actor, void(ActorType::*handler)(const ValueType &message, const Address from))
// 注销一个之前注册的消息处理函数
template<class ActorType , class ValueType >
bool DeregisterHandler (ActorType *const actor, void(ActorType::*handler)(const ValueType &message, const Address from))
// 检测是否跟定的消息处理函数被该actor所注册
template<class ActorType , class ValueType >
bool IsHandlerRegistered (ActorType *const actor, void(ActorType::*handler)(const ValueType &message, const Address from))
// 设置默认的消息处理函数,用来执行没有处理的消息
template<class ActorType >
bool SetDefaultHandler (ActorType *const actor, void(ActorType::*handler)(const Address from))
// 设置默认消息处理函数,用来执行没有处理的消息的重载
template<class ActorType >
bool SetDefaultHandler (ActorType *const actor, void(ActorType::*handler)(const void *const data, const uint32_t size, const Address from))
// 给一个给定地址的实体(actor或者Receiver)发送一条消息
template<class ValueType >
bool Send (const ValueType &value, const Address &address) const

3 Theron::Address类
 3.1 简介
  一个拥有第一无二地址的实体可以发送或者接收消息。地址在Theron中是所有实体独一无二的名称,可以接收消息。我们知道一个实体的地址,也就足够给它发送消息,并且不需要其他特殊的许可或者要求。
  在Theron中实体的类型拥有地址,并且可以接收消息,就是actors和receivers。这两种类型的实体在构造的时候就被自动分配了独一无二的地址。
  地址可以被拷贝和分配,允许actors和receivers的地址在消息中被发送传递。(但是现在还不可以通过发送给远方的actors,因为它们不可以通过memcpy被拷贝)。
 3.2 公共的成员函数

// 默认构造函数
 Address ()
// 显示构造函数
Address (const char *const name)
// 拷贝构造函数
Address (const Address &other)
// 赋值运算符
Address & operator= (const Address &other)
// 获取识别framework包含该实体的整数index
uint32_t GetFramework () const
// 获取地址的值为一个string
const char * AsString () const
// 获取地址的值为一个unsigned 32位整数型 
uint32_t AsInteger () const
// 获取地址的值为一个unsigned 64位整数型 
uint64_t AsUInt64 () const
// 等号运算符
bool operator== (const Address &other) const
// 不等号运算符
bool operator!= (const Address &other) const
    Inequality operator. More...
// 小于号运算符
bool operator< (const Address &other) const

 3.3 静态公共的成员函数

// 静态方法,用来返回第一无二的'null'地址
static const Address &  Null ()

4 Theron::AllocatorManager类
 4.1 简介
  这是一个管理内存分配的静态类。勒种静态方法SetAllocator和GetAllocator 允许非配器被Theron设置和回收。设置这个内存分配器是用来取代DefaultAllocator,而DefaultAllocator的功能是如果没有传统的分配器被显示设置则会调用它。GetAllocator方法返回一个指向当前设置的分配器的指针,这个分配器可能是之前通过SetAllocator设置的,或者是DefaaultAllocator类的一个实例(当没有任何分配器被设置)。使用参考如下,其实我们不是很常用到。

class MyAllocator : public Theron::IAllocator
{
public:
    MyAllocator();
    virtual ~MyAllocator();
    virtual void *Allocate(const SizeType size);
    virtual void *AllocateAligned(const SizeType size, const SizeType alignment);
    virtual void Free(void *const memory);
    virtual void Free(void *const memory, const SizeType size);
};
MyAllocator allocator;
Theron::AllocatorManager::SetAllocator(&allocator);

  SetAllocator方法可以在应用程序初始至多被调用一次。如果DefaultAllocator被一个传统分配器所取代,那么这个取代操作必须是在应用的起始阶段,必须在Theron对象(endpoints, frameworks,actors 和 receivers)构造之前。而GetAllocator在SetAllocator被调用之后就可以随意的被多次调用了。
 4.2 静态公共成员函数

// 设置分配器取代默认分配器用于内部内存分配
static void SetAllocator (IAllocator *const allocator)
// 通过Theron获取指向当前分配器的指针
static IAllocator * GetAllocator ()
// 获取隐藏通用分配的缓存分配的指针
static IAllocator * GetCache ()

5 Theron::Catcher< MessageType > 类模板
 5.1 简介
  template class Theron::Catcher< MessageType >这是一个特别实用的类模板,是用来捕获由Receiver收到的消息,模板参数是捕获的消息类型,这个类在前面实例中有详细的介绍过。
 5.2 公共成员函数

// 默认构造函数
Catcher ()
// 析构函数
~Catcher ()
// 如果catcher没有捕获到任何消息则返回true
bool Empty () const
// 推送一条消息到消息队列中
void Push (const MessageType &message, const Address from)
// 获取下一个捕获的消息,但是不会将其从队列中移除
bool Front (MessageType &message, Address &from)
// 获取下一个捕获的消息,并且会将其从队列中移除
bool Pop (MessageType &message, Address &from)

6 小结  
  Theron框架其实并不是非常复杂,其包含的类也不是特别的多,一共仅有十余个,剩下的后面博文再介绍。
  以上是个人学习记录,由于能力和时间有限,如果有错误望读者纠正,谢谢!
  转载请注明出处:http://blog.csdn.net/FX677588/article/details/75330473


 参考文献:
 Theron框架官网http://www.theron-library.com/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值