-2

i am writing a program in sbcl to multiply two numbers using add and shift method. while my normal program would run nicely but when i use threads , the program shows no output and no error message. Is there any mistake in code which i should remove.

(use-package :sb-thread)
(defvar *buffer-queue* (make-waitqueue))
(defvar *buffer-lock* (make-mutex :name "buffer-lock"))
(defvar *buffer* (list nil))

(defun writer()
    (let ((res 0))
        (loop for lpr from 0 to 63
              do (let ((end-bit (logand num2 1)))
                (with-mutex (*buffer-lock*)
                    (setf *buffer* (cons end-bit *buffer*))
                    (setq num2 (ash num2 -1))
                    (condition-notify *buffer-queue*)
                )))))

(defun reader()
    (let ((end-bit 0) (res 0))
    (with-mutex (*buffer-lock*)
        (loop
        (condition-wait *buffer-queue* *buffer-lock*)
            (loop
                (unless *buffer* (return))
                (end-bit (car *buffer*))
                (setf *buffer* (cdr *buffer*)))))
    (if (= end-bit 1)
                   (setq res (+ res num1)))
        (setq num1 (ash num1 1))
        (format t "result is ~a.~%" res)
    )
)





(let ((num1 (progn
              (write-line "Enter first number: ")
              (finish-output)
              (read)))
      (num2 (progn
              (write-line "Enter second number: ")
              (finish-output)
              (read))))
  (if (or (= num1 0) (= num2 0))
      (write-line "result is 0.0")
      (calculator num1 num2))

)

why it is happening?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Amit Chahar
  • 2,519
  • 3
  • 18
  • 23
  • You may want to indent your code and place the parentheses differently. It's also not clear why you tag it as 'elisp' (Emacs Lisp) or 'clisp' (GNU CLISP). The language is called Common Lisp. SBCL is the implementation you are using. Where are the threads, example, error message? – Rainer Joswig Jan 04 '15 at 11:34
  • You also might want to spell `DEFVAR` correctly. `DEVAR` is probably not what you wanted to write. – Rainer Joswig Jan 04 '15 at 11:36
  • ok, i have corrected the spelling , but it is still not running. And showing no error. – Amit Chahar Jan 04 '15 at 11:48
  • 1
    You never use `writer` nor `reader` in the code shown, and `calculator` also seems to be defined elsewhere (I guess that calling `end-bit` as a function is simply a mistake). If you do not see an error, this is not the complete example. – Svante Jan 04 '15 at 14:19

1 Answers1

1

To figure out what is happenning, I strongly suggest to use (trace writer) and (trace reader) (maybe even (trace calculator)).

I would also suggest to use bordeaux-thread which is simply a shim (bordeaux = shim in french) to make threading works on multiple implementation.

fstamour
  • 760
  • 4
  • 9