什么是语义含义:volatile-mutable与:unsynchronized-mutable? [英] What are the semantic implications of :volatile-mutable versus :unsynchronized-mutable?

查看:196
本文介绍了什么是语义含义:volatile-mutable与:unsynchronized-mutable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习一个clojure lib,当我注意到一个可变字段注释了 ^:unsynchronized-mutable 。 mutable是可变的,但我不知道不同步部分的含义,所以我阅读了 docs ,其中包含:

I was studying a clojure lib when I noticed that a mutable field was annotated with ^:unsynchronized-mutable. Mutable is mutable, but I had no idea what the unsynchronized part meant, so I read the docs, which contain:


请注意,可变字段极难使用
正确,并且仅仅是为了便于在Clojure
本身中构建更高的
级别构造,例如Clojure的引用类型。它们仅供专家使用 - 如果语法和意义
:volatile-mutable或:unsynchronized-mutable不立即显示
,您就不应该使用它们。

Note well that mutable fields are extremely difficult to use correctly, and are present only to facilitate the building of higher level constructs, such as Clojure's reference types, in Clojure itself. They are for experts only - if the semantics and implications of :volatile-mutable or :unsynchronized-mutable are not immediately apparent to you, you should not be using them.

我无法得到细微差别:它是说,在实践中,我选择哪个可变性注释无关紧要,或者人们应该忘记使用可变类型?

I couldn't get the nuance: is it saying that in practice it doesn't matter which mutability annotation I choose, or that people should just forget about using mutable types altogether?

并且,为了好奇,在较低的抽象层次上,它们之间的语义差异是什么?

And, for the sake of curiosity, in a lower level of abstraction, what is the semantic difference between them?

谢谢!

推荐答案

这是说如果你不明白: volatile-mutable :unsynchronized-mutable 你应该使用Clojure的引用类型,而不是直接使用可变字段。

It is saying that if you don't understand the difference between :volatile-mutable and :unsynchronized-mutable you should use Clojure's reference types rather than directly using mutable fields.

这两种可变性具有不同的策略,用于保证共享可变数据的线程之间的一致性,并且这导致读/写操作的不同性能。有时,您可以通过精细调整特定类型的可变性的使用来获得更好的性能。如果你天真地使用它们,你可能会很奇怪,很难理解错误。

These two kinds of mutability have different strategies for guaranteeing consistency between threads for shared mutable data, and this results in different performance of read / write operations. Sometimes you can get better performance by fine tuning for usage of a specific kind of mutability. You can get weird and hard to understand bugs if you use them naively.

这篇关于什么是语义含义:volatile-mutable与:unsynchronized-mutable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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