我有一个用于缓存值的简单函数。我使用哈希表作为缓存。缓存将从不同的线程访问,我想使用atomic boxes来控制从不同线程的访问。到目前为止,我已经有了以下代码
; Define the local hash table we will use.
(define cache (make-atomic-box (make-hash-table)))
(define-public (make-cache ITEM fv)
(let ((val (hash-ref (atomic-box-ref cache) ITEM)))
(if val
val
(begin
(hash-set! cache ITEM fv) ;;this where I want to use atomic box update functions
fv
))
)
)现在,我不清楚如何将一个原子盒更新函数(atomic-box-set!、atomic-box-swap!或atomic-box-compare-and-swap! )包装在hash-set函数周围,以便向哈希表添加新值。
那么我如何使用原子盒来更新哈希表呢?
发布于 2020-05-20 05:01:31
由于散列是可变的,您可以简单地将对散列和框的调用堆叠为(hash-set! (atomic-box-ref cache) ITEM fv)。
如果要处理不可变值,则需要将该值复制出来(atomic-box-ref),并将处理后的值单独写入(atomic-box-set!)。atomic-box-swap!将这两个操作合并到一个操作中。
https://stackoverflow.com/questions/61456217
复制相似问题