title: Printing to the Terminal from (parallel) Threads (Common Lisp)
body: <p>In one of Timmy Jose's blog posts at <a href="https://z0ltan.wordpress.com/2016/09/02/basic-concurrency-and-parallelism-in-common-lisp-part-3-concurrency-using-bordeaux-and-sbcl-threads/" rel="nofollow noreferrer">https://z0ltan.wordpress.com/2016/09/02/basic-concurrency-and-parallelism-in-common-lisp-part-3-concurrency-using-bordeaux-and-sbcl-threads/</a> he gives an example of the wrong way to print to the top level from inside a thread (using Bordeaux Threads as an example, although I am using Lparallel):</p>
  
  <pre><code>(defun print-message-top-level-wrong ()
    (bt:make-thread
      (lambda ()
        (format *standard-output* "Hello from thread!")))
    nil)
  
  (print-message-top-level-wrong) -&gt; NIL
  </code></pre>
  
  <p>The explanation is that "The same code would have run fine if we had not run it in a separate thread. What happens is that each thread has its own stack where the variables are rebound. In this case, even for <code>*standard-output*</code>, which being a global variable, we would assume should be available to all threads, is rebound inside each thread!"</p>
  
  <p>And this is exactly what happens if the function is run in Allegro CL. However, in SBCL the function does print the intended output at the terminal. Does this mean <code>*standard-output*</code> is not being rebound in SBCL? In general, is there a cross-platform way to print to <code>*standard-output*</code> from inside a thread?</p>
  
  <p>In a multithreaded situation printing to the terminal should normally be coordinated to avoid potentially printing from several streams at the same time. But there don't seem to be any functions like <code>atomic-format</code> or <code>atomic-print</code> available. Is there a straightforward way to avoid printing interference when there are multiple threads (assuming that locks/mutexes are too expensive to use for each individual printing operation)?</p>