SBCL 中多线程的原子操作
Atomic Operations for Multithreading in SBCL
加载包含原子操作的函数时出现错误。举个简单的例子,文件 test.lisp 包含:
(defparameter *count* 0)
(defun test ()
(sb-ext:atomic-incf *count*))
生成以下错误:
* (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
*
为什么*count*
无效?
来自文档字符串:
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.
我怀疑这些是为了避免在进行比较和交换时进行运行时检查。
要回答上面关于如何将 atomic-incf
与声明的全局变量一起用作 fixnum
的问题,这对我有用:
(declaim (fixnum **var**))
(sb-ext:defglobal **var** 0)
(sb-ext:atomic-incf **var**)
(defparameter *count* (list 0))
(defun test ()
(sb-ext:atomic-incf (car *count*)))
(test)
加载包含原子操作的函数时出现错误。举个简单的例子,文件 test.lisp 包含:
(defparameter *count* 0)
(defun test ()
(sb-ext:atomic-incf *count*))
生成以下错误:
* (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
*
为什么*count*
无效?
来自文档字符串:
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.
我怀疑这些是为了避免在进行比较和交换时进行运行时检查。
要回答上面关于如何将 atomic-incf
与声明的全局变量一起用作 fixnum
的问题,这对我有用:
(declaim (fixnum **var**))
(sb-ext:defglobal **var** 0)
(sb-ext:atomic-incf **var**)
(defparameter *count* (list 0))
(defun test ()
(sb-ext:atomic-incf (car *count*)))
(test)