Actor与线程相比如何工作? [英] How does Actors work compared to threads?

查看:775
本文介绍了Actor与线程相比如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与主题相比,演员有什么好的和简短的解释吗?

Is there any good and short explanation of how Actors works compared to threads?

线程不能被视为演员并发送消息到其他线程?我看到一些区别,但对我来说不是很清楚。

Can't a thread be seen as an actor and send messages to other threads? I see some difference, but it's not that clear for me. Can I use Actors in any language by using threads differently?

推荐答案

演员模型对消息操作通过。允许单个进程(actor)异步地相互发送消息。与我们通常认为的线程模型不同的是,(理论上至少)没有共享状态。如果一个人认为(合理地,我认为)共享的国家是所有邪恶的根源,那么演员模型变得非常有吸引力。

The actor model operates on message passing. Individual processes (actors) are allowed to send messages asynchronously to each other. What distinguishes this from what we normally think of as the threading model, is that there is (in theory at least) no shared state. And if one believes (justifiably, I think) that shared state is the root of all evil, then the actor model becomes very attractive.

我们不应该过度兴奋,但是。演员模型不(与一些指控相反)不可能有死锁。 actor模型也不会阻止您在不同进程之间争用资源 - 例如消息队列。该模型只有锁定高于一定水平。在较低级别,为了协调消息队列,仍然需要锁定。

We shouldn't get over excited, however. The actor model does not (contrary to some allegations) make it impossible to have deadlocks. The actor model also does not prevent you from having contention for resources between different processes -- message queues, for instance. The model is only "lock-free" above a certain level. At a lower level, for coordinating message queues, locking is still required.

线程不能被看作一个actor并向其他线程发送消息?

好,是和否。不,如果你只是使用在共享内存位置放置mutex的方法。然后线程共享这个状态 - 它们都可以访问这个内存,可以读取它,重写它等。但是你可以在线程模型上构建一个actor模型,事实上所有的actor实现都有线程下。我已经黑客一起这样的东西(非常糟糕)通过给每个线程一个互斥体守卫的队列 - 只是为了乐趣。要了解如何管理actor-thread阻抗,请参阅我的问题从一年前

Well, yes and no. No, if you're just using the approach of putting mutexes around shared memory locations. Then the threads share this state -- they both have access to this memory, can both read it, re-write it, etc. But you can build an actor model on top of a threading model, and indeed all actor implementation do have threads underneath. I've hacked together something like this (very badly) by giving each thread a queue guarded by a mutex -- just for fun. To get an idea of how actor-thread impedance is managed, see my question from a year ago.

我可以使用任何语言的Actor模型使用不同的线程

是的,但这需要更多的工作。你最喜欢的语言可能有一个消息传递库,所以这将是第一件调查。此外,您应该调查不可变数据结构的使用。注意,如果数据结构是不可变的,那么你基本上处理了共享状态问题 - 多个线程可以保存对不可变数据的引用,而不会发生任何坏事。有一个原因为什么演员的语言也倾向于是功能语言(erlang,scala)。

Yes, but it'll take a bit more work. Your favourite language may well have a message-passing library, so that would be the first thing to investigate. Also, you should investigate the use of immutable data structures. Notice that if a data structure is immutable, then you've essentially dealt with the "shared-state" problem -- multiple threads can hold references to immutable data without anything bad happening. There's a reason why actor languages tend to also be functional languages (erlang, scala).

您可能还想看看软件事务内存,这是一个不同的但也引人注目的模型。 Clojure是我最喜欢的例子。

You may also want to have a look at Software Transactional Memory, which is a different but also compelling model. Clojure is my favourite example of that.

这篇关于Actor与线程相比如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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