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

问题描述

我一直在寻找学习erlang,因此,一直在阅读(好,撇去)关于演员模型。



从我的理解, actor模型只是一组函数(在erlang中称为processes的轻量级线程中运行),它们仅通过消息传递来相互通信。



在C ++或任何其他语言中实现:

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

每个进程都是派生BaseActor的实例。参与者仅通过消息传递彼此通信。 (即,推动)。



现在,我明白我缺少了一个中心地图,或者,更重要的是,这里的一个重要问题,即:
缺乏屈服意味着一个单一的演员可以不公平地消耗过多的时间。但是跨平台协同程序是使这在C ++的困难的主要东西吗? (Windows例如有光纤。)



还有什么我缺少,或者是模型真的这么明显吗?



我绝对不是在这里开始一场火焰战争,我只是想了解我错过了什么,因为这本质上是我已经做了能够对并发代码进行一些推理。 / p>

解决方案

C ++代码不处理公平,隔离,故障检测或分布,这些都是Erlang作为其一部分演员模型。




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

  • 如果一个演员崩溃,

  • 如果一个actor崩溃,其他actor应该能够检测到并响应该崩溃(错误检测)


  • b> 使演员的JIT调度,将它们移动到核心,这是目前具有最少利用率的核心,并且还休眠某些核心上的线程,如果它们不再需要的话。



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



    这些东西在C ++中是不可能的,但如果你添加一个事实,Erlang几乎所有主要的hw和os配置的工作,他们变得越来越困难。



    编辑:刚刚找到关于他看到的erlang风格并发性。


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

    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.

    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;
    }
    

    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.

    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?

    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.

    解决方案

    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)

    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.

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

    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.

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

    这篇关于演员模型:为什么erlang特别?或者,你为什么需要另一种语言呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:54