Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure:在一个事务中,更改之前的通勤会导致事务失败_Clojure_Transactions_Stm - Fatal编程技术网

Clojure:在一个事务中,更改之前的通勤会导致事务失败

Clojure:在一个事务中,更改之前的通勤会导致事务失败,clojure,transactions,stm,Clojure,Transactions,Stm,我是Clojure的新手,一直在尝试理解它的事务模型。 在使用alter和comment时,我注意到如果在comment之后修改ref,那么事务将不会提交任何内容或对任何内容都不做任何更改 例如: (def counter (ref 0)) (def i (ref 0)) (future (dosync (ref-set counter 1) (ref-set i 1) (commute counter inc) (

我是Clojure的新手,一直在尝试理解它的事务模型。 在使用alter和comment时,我注意到如果在comment之后修改ref,那么事务将不会提交任何内容或对任何内容都不做任何更改

例如:

(def counter (ref 0))
(def i (ref 0))
(future (dosync
          (ref-set counter 1)
          (ref-set i 1)
          (commute counter inc)
          (alter counter inc)))
@counter和@i都将为0,但如果我交换通勤和更改,或者在这种情况下使用两个通勤或两个更改,它将分别产生所需的结果3和1

我读过一些帖子解释说,Communion和alter的行为有点不同,因为Communion实际上在一个事务中执行了两次,一次是在它所在的位置,另一次是在提交阶段,并忽略了ref的不一致快照。我只是被这两个组合的奇怪行为弄糊涂了

有人能解释一下它是如何工作的吗?提前谢谢

通勤功能仅在非常狭窄的情况下有用,即在极少数情况下,它可能会以更新功能的额外重试为代价来减少锁争用。这也使得交易的心理模型更加复杂,例如,你的例子表明我以前从未见过这个问题


依我看,使用alter而不是通勤几乎总是更好的,因为alter更简单、更防弹。事实上,我通常会考虑使用通勤是一个过早优化的情况。

Culjule: 1.8在Rep{{状态:失败::ValError {:原因不能在通勤后设置:通过{类型java. UTI.CONCURNET.ExcExcExpRebug……所以不要在通勤后改变。找不到任何具体的DOC——只是