首页 > 解决方案 > Atomic Operations for Multithreading in SBCL

问题描述

I'm getting an error when loading a function that contains an atomic operation. As a simple example, the file test.lisp contains:

(defparameter *count* 0)
(defun test ()
  (sb-ext:atomic-incf *count*))

which generates the following error:

* (load "d:\\test.lisp")

; file: d:/test.lisp
; in: DEFUN TEST
;     (ATOMIC-INCF *COUNT*)
;
; caught ERROR:
;   during macroexpansion of (ATOMIC-INCF *COUNT*). Use *BREAK-ON-SIGNALS* to
;   intercept.
;
;    Invalid first argument to ATOMIC-INCF: *COUNT*
;
; compilation unit finished
;   caught 1 ERROR condition
T
*

Why is *count* invalid?

标签: multithreadingcommon-lispsbcl

解决方案


从文档字符串:

PLACE must access one of the following:
 - a DEFSTRUCT slot with declared type (UNSIGNED-BYTE 64)
   or AREF of a (SIMPLE-ARRAY (UNSIGNED-BYTE 64) (*))
   The type SB-EXT:WORD can be used for these purposes.
 - CAR or CDR (respectively FIRST or REST) of a CONS.
 - a variable defined using DEFGLOBAL with a proclaimed type of FIXNUM.
Macroexpansion is performed on PLACE before expanding ATOMIC-INCF.

我怀疑这些是为了在进行比较和交换时避免运行时检查。


推荐阅读