应该在事务中读取多个Clojure引用以确保一致性吗? [英] Should multiple Clojure refs be read in a transaction for consistency?

查看:164
本文介绍了应该在事务中读取多个Clojure引用以确保一致性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个理论问题,我想要更好地理解Clojure的并发性。

This is a theoretical question motivated by my desire to understand Clojure's concurrency better.

假设我正在编写boids。假设每个boid是一个单独的绿线代替向量中的位置或代表世界网格的refs。认为Hickey的蚂蚁殖民地。

Let's say I'm writing boids. Assume each boid is a separate green thread mutating positions in a vector or refs representing a world grid. Think Hickey's ants colony.

现在,Clojure.org的文档声明Refs的所有读取将看到一个Ref world事务(其读取点)。

Now, documentation at Clojure.org states "All reads of Refs will see a consistent snapshot of the ‘Ref world’ as of the starting point of the transaction (its ‘read point’)."

这是否意味着我只能获得我的模拟的一致性快照,例如绘制它,通过在事务中读取refs的向量(即在dosync上下文?)

Does this mean I can only get a consistent snapshot of my simulation, for example to draw it, by reading my vector of refs within a transaction (i.e. within a dosync context?)

谢谢!

推荐答案

需要一个一致的快照。

如果您在事务之外读取参考,那么您将在读取每一个时刻获得一个即时值。您不能保证其他交易不会在您的阅读之间更改一个或多个参考,因此您最终可能会出现不一致的视图。

If you read the refs outside a transaction, then you will just get an instantaneous value at the moment that you read each one. You have no guarantee that another transaction will not change one or more of the refs in between your reads, so you could end up with an inconsistent view.

这篇关于应该在事务中读取多个Clojure引用以确保一致性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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