anchor.diff:      thread))
   
   ;; ----------------------------------------------------------------------------
  -;; (defun get-add (actor) (second actor))
  -(defmethod get-add ((self actor))
  -  "Returns handle to the add function"
  -  (with-slots (add) self
  -    add))
  +;; The main which is started as a thread from the constructor I think that this
  +;; should be more of an internal function than a method (experiment with
  +;; funcallable-standard-class)
  +(defmethod main((self actor))
  +  (with-slots (lock cv (behav behavior) messages) self
  +    (loop
  +       (thread-yield)
  +       (with-lock-held (lock)
  +         (if (not (null messages))
  +             (setf behav (apply behav
  +                                (pop messages)))
  +             (condition-wait cv lock ))
  +         (unless behav (return))))))
   
   ;; ----------------------------------------------------------------------------
   ;; Create a behavior that can be attached to any actor
  
positive.code: ;; ----------------------------------------------------------------------------
  ;; The shell of an actor
  (defun make-actor (behav name)
    (let (self
          (lock (bt:make-lock))
          (messages '())
          (cv (bt:make-condition-variable)))
      (labels ((add (m)
                 (with-lock-held (lock)
                   (setf messages (nconc messages (list m))))
                 (condition-notify cv))
               (run-actor () (loop
                                (thread-yield)
                                (with-lock-held (lock)
                                  (if (not (null messages))
                                      (setf behav (apply behav
                                                         (pop messages)))
                                      (condition-wait cv lock ))
                                  (unless behav (return))))))
        (setf self (make-instance 'actor
                                  :name (concatenate 'string "Actor: " name)
                                  :main #'run-actor
                                  :add #'add
                                  :messages messages)))))
  
  ;; ----------------------------------------------------------------------------
  (defun if-single (x)
  
negative.code:          :initform (error ":name must be specified")
           :accessor name
           :documentation "Hold the name of actor")
     (behavior :initarg :behavior
           :initform (error ":behav must be specified")
           :accessor behavior
           :documentation "Behavior")
     (messages :initform '()
           :accessor messages
           :documentation "Message stream sent to actor")
     (lock :initform (bt:make-lock)
           :accessor lock
           :documentation
           "The lock is used when adding a message to the message queue")
     (cv :initarg :cv
           :initform (bt:make-condition-variable)
           :accessor cv
           :documentation "conditional variable used by the thread")
     thread))
  
  ;; ----------------------------------------------------------------------------
  (defmethod initialize-instance :after((self actor) &key)
    "Uses the main functiona name to create a thread"
    (with-slots (name thread) self
      (setf thread
            (bt:make-thread #'(lambda() (main self))
                            :name name))))
  
  ;; ----------------------------------------------------------------------------
  (defmethod send ((self actor) &rest message)
    "
  Creates a message sending thread which
  1. Holds lock to the message (queue)
  2. Appends messages (queue) with incoming message
  3. Releases lock
  4. Notifies the waiting thread that there is a message
  "
    (with-slots (messages lock cv) self
      (bt:make-thread #'(lambda ()
                          (with-lock-held (lock)
                            (setf messages (nconc messages (list message))))
                          (condition-notify cv)))
      (values)))
  
  ;; ----------------------------------------------------------------------------
  ;; (defun stop-actor (actor) (destroy-thread (get-thread actor)))
  