与线程相比,Actor 的工作原理如何?

发布于 2024-09-16 09:12:07 字数 131 浏览 10 评论 0原文

与线程相比,对于Actors如何工作有什么好的和简短的解释吗?

线程不能被视为参与者并向其他线程发送消息吗?我看到了一些差异,但对我来说并不是那么清楚。我可以通过不同方式使用线程来在任何语言中使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-09-23 09:12:07

参与者模型基于消息传递进行操作。允许各个进程(参与者)彼此异步发送消息。这与我们通常认为的线程模型的区别在于(至少在理论上)没有共享状态。如果人们相信(我认为这是合理的)共享状态是万恶之源,那么参与者模型就会变得非常有吸引力。

然而,我们不应该过于兴奋。演员模型并没有(与某些指控相反)使僵局成为不可能。参与者模型也不会阻止您在不同进程(例如消息队列)之间争夺资源。该模型只有在一定级别以上才“无锁”。在较低级别,为了协调消息队列,仍然需要锁定。

一个线程不能被视为参与者并向其他线程发送消息吗?

嗯,是的,也不是。不,如果您只是使用在共享内存位置周围放置互斥体的方法。然后线程共享这个状态——它们都可以访问这个内存,都可以读取它,重写它,等等。但是你可以在线程模型之上构建一个actor模型,实际上所有actor实现都有线程下。我通过为每个线程提供一个由互斥锁保护的队列来将类似的东西(非常糟糕)组合在一起——只是为了好玩。要了解如何管理参与者线程阻抗,请参阅 我一年前的问题

我可以通过以不同的方式使用线程来在任何语言中使用 Actor 模型吗?

可以,但这需要更多的工作。您最喜欢的语言很可能有一个消息传递库,因此这将是首先要研究的事情。另外,您应该研究不可变数据结构的使用。请注意,如果数据结构是不可变的,那么您基本上已经处理了“共享状态”问题——多个线程可以保存对不可变数据的引用,而不会发生任何不良情况。 Actor 语言往往也是函数式语言(erlang、scala)是有原因的。

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

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.

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.

Can't a thread be seen as an actor and send messages to other threads?

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.

Can I use the Actor Model in any language by using threads differently?

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).

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.

临走之时 2024-09-23 09:12:07

我不会说参与者总是异步传递消息——那太慢了。举个例子,JActor 项目使用 2 路消息(请求/响应)来更好地建模方法调用。大多数请求都是同步处理的。

JActor(一个 Java 库)也不使用锁。只有一些原子和并发数据结构,并带有一些信号量。消息传递约为每秒 8 亿条消息。

https://github.com/laforge49/JActor

I wouldn't say that actors always pass messages asynchronously--that would be too slow. Case in point, the JActor project uses 2-way messages (request/response) to better model a method call. And most requests are serviced synchronously.

JActor (a Java library) also does not use locks. Only some atomic and concurrent data structures, with a few semaphores thrown in. Message passing is about .8 Billion messages per second.

https://github.com/laforge49/JActor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文