mp:with-lock
— Synchronize a piece of code between different tasks.
(mp::with-lock
(lock)
&body
body)
It grabs a lock, executes a piece of lisp code and releases the lock at the end. The inner forms are protected so that when any condition is signalled the lock is released.
Ensure each task increments the counter properly. The lock is required because INCF is not an atomic operation.
(defvar *counter* 0) (defvar *counter-lock* (mp:make-lock :name 'counter)) (flet ((task (name) (loop while (<= *counter* 10) do (progn (sleep 1) (with-lock (*counter-lock*) (format t "~%;;; ~A counts ~D" name *counter*) (terpri) (incf *counter*)))))) (mp:process-run-function 'ana #'task 'ana) (mp:process-run-function 'jose #'task 'jose))