演员模型:为什么Erlang / OTP特别?你能用另一种语言吗? [英] The actor model: Why is Erlang/OTP special? Could you use another language?

查看:115
本文介绍了演员模型:为什么Erlang / OTP特别?你能用另一种语言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Erlang / OTP,因此,一直在阅读(好的,略读一下)演员模型。

I've been looking into learning Erlang/OTP, and as a result, have been reading (okay, skimming) about the actor model.

可以理解,参与者模型只是一组功能(在Erlang / OTP中称为进程的轻量级线程中运行),它们仅通过消息传递相互通信。

From what I understand, the actor model is simply a set of functions (run within lightweight threads called "processes" in Erlang/OTP), which communicate with each other only via message passing.

用C ++或任何其他语言来实现似乎很简单:

This seems fairly trivial to implement in C++, or any other language:

class BaseActor {
    std::queue<BaseMessage*> messages;
    CriticalSection messagecs;
    BaseMessage* Pop();
public:
    void Push(BaseMessage* message)
    {
        auto scopedlock = messagecs.AquireScopedLock();
        messagecs.push(message);
    }
    virtual void ActorFn() = 0;
    virtual ~BaseActor() {} = 0;
}

每个过程都是派生BaseActor的实例。角色之间仅通过消息传递进行通信。 (即推动)。参与者在初始化时会在中心地图上注册自己,这样其他参与者就可以找到他们,并允许中心函数通过它们运行。

With each of your processes being an instance of a derived BaseActor. Actors communicate with each other only via message-passing. (namely, pushing). Actors register themselves with a central map on initialization which allows other actors to find them, and allows a central function to run through them.

现在,我知道我不见了,或者说,在这里掩盖了一个重要问题,即:
缺乏收益意味着单个Actor会不公平地消耗过多时间。但是,跨平台协程是C ++中使这一点变得困难的主要因素吗? (例如,Windows具有纤维。)

Now, I understand I'm missing, or rather, glossing over one important issue here, namely: lack of yielding means a single Actor can unfairly consume excessive time. But are cross-platform coroutines the primary thing that makes this hard in C++? (Windows for instance has fibers.)

我是否还缺少其他任何东西,或者该模型真的很明显吗?

Is there anything else I'm missing, though, or is the model really this obvious?

推荐答案

C ++代码不涉及公平,隔离,故障检测或分发,而Erlang作为其参与者模型的一部分带来了所有这些事情。

The C++ code does not deal with fairness, isolation, fault detection or distribution which are all things which Erlang brings as part of its actor model.


  • 不允许演员饿死任何其他演员(公平)

  • 如果一个演员崩溃,它只会影响那个参与者(隔离)

  • 如果一个参与者崩溃,其他参与者应该能够检测到该崩溃并对之做出反应(故障检测)

  • Actor应该能够通过网络进行通信,就像他们在同一台计算机上一样(分布)

  • No actor is allowed to starve any other actor (fairness)
  • If one actor crashes, it should only affect that actor (isolation)
  • If one actor crashes, other actors should be able to detect and react to that crash (fault detection)
  • Actors should be able to communicate over a network as if they were on the same machine (distribution)

Beam SMP仿真器带来参与者的JIT调度,将他们移动到当前利用率最低的核心,并且如果不再需要某些核心,则使它们休眠。

Also the beam SMP emulator brings JIT scheduling of the actors, moving them to the core which is at the moment the one with least utilization and also hibernates the threads on certain cores if they are no longer needed.

此外,所有用Erlang编写的库和工具都可以假定这是世界运作的方式,并进行了相应的设计。

In addition all the libraries and tools written in Erlang can assume that this is the way the world works and be designed accordingly.

这些事情在C ++中并非并非不可能,但是如果您加上一个事实,那就是Erlang可以在几乎所有主要的硬件和操作系统配置上工作,那么,这些事情就会变得越来越困难。

These things are not impossible to do in C++, but they get increasingly hard if you add the fact that Erlang works on almost all of the major hw and os configurations.

编辑:刚刚通过 Ulf Wiger 关于他认为erlang样式并发的看法。

edit: Just found a description by Ulf Wiger about what he sees erlang style concurrency as.

这篇关于演员模型:为什么Erlang / OTP特别?你能用另一种语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆