Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBCL multiple threads write to standard-output

I wrote a server that spins off new threads. Some of those threads need to write to standard-output, but when they do, nothing shows up in terminal.

Is there some type of messaging api in sbcl that allows me to send messages back to the main thread?

Thanks much!

like image 451
myselfesteem Avatar asked Nov 27 '14 21:11

myselfesteem


1 Answers

You need to pass the current *standard-output* to the new thread somehow, then in that thread's function, you can bind *standard-output* to that value.

Current Common Lisp implementations make thread-local dynamic bindings, and SBCL is one of them.

(sb-thread:make-thread ;; thread function
                       #'(lambda (standard-output)
                           ;; thread-local dynamic binding of special variable
                           (let ((*standard-output* standard-output))
                             ...))
                       ;; thread function argument, provided by the current thread
                       :arguments (list *standard-output*))

Note that I could have named the thread function's argument *standard-output* and then I wouldn't need the let, since the dynamic binding was done at function entry. But I think that dynamic bindings should be explicit and obvious, notwithstanding the earmuffs around special variable naming convention.

like image 165
acelent Avatar answered Nov 04 '22 10:11

acelent