macrolyte wrote:A couple more questions (please see code below):
Code: Select all
;; is this a correct way for this?
(defun test() ;; Is the general pattern to use a defun? Couldn't you use the lambda body in the thunk in make-thread just as well?
(bt:with-lock-held (lock)
(do-stuff)
(do-more-stuff)
(bt:condition-wait cv lock))) ;; Should the call to condition-wait be within the body of with-lock-held or not?
The order of bt:condition-wait and do-stuff seems to be reversed, one usually waits on a condition variable being signalled before doing something about it.
macrolyte wrote:Also, could someone please tell me where the output is going when I do this?
Code: Select all
(defvar *k0* nil)
(setf *k0* (bt:make-thread ;; capture returned thread object
(lambda()
(unwind-protect (loop) ;;
(print "Goodbye"))) :name "Say goodbye")) ;; With or without the #.*standard-output*, I get no output... prints fine without a loop.
Hopefully it isn't a
SBCL thing. Thanks.
The code as it stands here above does not produce any output, at least not yet. The form (loop) being the degenerate version of the simple variant of the cl:loop macro it denotes an infinitely busy empty loop that does nothing more than waste CPU cycles very quickly and for ever. (In C you could write it as "for (;;);" or as "while (1);")
So the thread you thus created spins without any output until something kicks it out of its merry looping. And that was the reason for the process-kill form wrapped around the original piece of code you took from "Lisp Outside the Box". In Allegro CL, AFAICT, the function process-kill when applied to a thread will interrupt its execution, force it to unwind its call stack and bring it to a state of terminal inactivity. It is during the unwinding of the thread's call stack that 'Goodbye' will end up printed on a stream.
In Bordeaux Threads you may try to use bt:destroy-thread on your *k0* thread to achieve the same result but beware that its specification clearly states that it is implementation-defined whether the thus targetted thread will have its call stack unwound or not. On SBCL it happens to be the case. On other implementations you may try to use (bt:interrupt-thread *k0* #'(lambda () (abort)) instead in order to force the thread's call stack unwinding that you expect.
But there is still that question of where is 'Goodbye' printed and, mainly, why there.