演员模特:为什么erlang特别?或者,为什么你需要另外一种语言呢? [英] The actor model: Why is erlang special? Or, why do you need another language for it?

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

问题描述

我一直在研究学习erlang,结果,一直在阅读(好的,撇去)演员模特。

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

从我所了解的, actor模型只是一组函数(在erlang中的轻量级线程中称为进程),它们只能通过消息传递相互通信。

From what I understand, the actor model is simply a set of functions (run within lightweight threads called "processes" in erlang), 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?

我绝对不会在这里开始一场火焰战,我只是想了解我所缺少的东西,因为这本质上是我已经做的,可以有一些关于并发代码的原因。 / p>

I'm definitely not trying to start a flame war here, I just want to understand what I'm missing, as this is essentially what I already do to be able to somewhat reason about concurrent code.

推荐答案

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.


  • 没有演员可以饿死任何其他演员(公平)

  • 如果一个演员崩溃,它应该只影响该演员(隔离)

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

  • <演员应该能够通过网络进行通信,就像他们在同一台机器上一样(分配)
  • 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)

另外,光束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几乎所有主要的hw和os配置,

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特别?或者,为什么你需要另外一种语言呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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