Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Concurrency 为什么要交换!即使原子';s值为';你没变吗?_Concurrency_Clojure - Fatal编程技术网

Concurrency 为什么要交换!即使原子';s值为';你没变吗?

Concurrency 为什么要交换!即使原子';s值为';你没变吗?,concurrency,clojure,Concurrency,Clojure,假设我有以下一段(人为的)Clojure代码: (def c (clojure.lang.Atom. [nil nil])) (swap! c (fn [[x y]] ["done", (second (swap! c (fn [[x y]] [x y])))])) 我希望这项工作如下: Clojure取消装箱c以查找[nil nil],并将其传递给外部fn 外部fn调用交换,它取消装箱c以查找[nil nil],并将此值传递给内部fn 内部fn返回[nil nil]。

假设我有以下一段(人为的)Clojure代码:

(def c (clojure.lang.Atom. [nil nil]))
(swap! c 
   (fn [[x y]] 
       ["done", (second (swap! c (fn [[x y]] [x y])))]))
我希望这项工作如下:

  • Clojure取消装箱
    c
    以查找
    [nil nil]
    ,并将其传递给外部
    fn

  • 外部
    fn
    调用
    交换
    ,它取消装箱
    c
    以查找
    [nil nil]
    ,并将此值传递给内部
    fn

  • 内部
    fn
    返回
    [nil nil]
    。对
    交换的内部调用将该值替换为
    c
    的新值

  • 外部
    fn
    返回值
    [“done”nil]

  • 外部
    交换尝试
    比较和设置,并看到
    c
    的当前值
    [nil nil]
    与旧值
    [nil nil]
    相同,因此它成功地在
    [“done”nil]
    中交换

  • 但事实上,这段代码永远循环:外部循环不断重试


    为什么会这样?我的心智模型缺少了什么?

    Clojure Slack上的几个人帮助我了解了这里发生了什么(谢谢!)<代码>比较和设置
    使用
    相同?
    (即Java引用相等)来比较atom的新旧值。每次我重新创建向量时,我都会创建一个新对象,它与旧对象不同。即使我没有想到内部
    交换