Common Lisp Tips and Tricks

A collection of simple tips and tricks for the inexperienced Common Lisp programmers like myself.


List (and terminate) running threads

Using SBCL native functions

;; list all - mind the names
CL-USER> (sb-thread:list-all-threads)
(#<SB-THREAD:THREAD "main thread" RUNNING {1001878913}>
 #<SB-THREAD:THREAD "auto-flush-thread" RUNNING {10020BFDB3}>
 #<SB-THREAD:THREAD "my main loop" RUNNING {100492B8E3}>   ;; this is the thread i suspect 
 #<SB-THREAD:THREAD "reader-thread" RUNNING {1004AEFD13}>
 #<SB-THREAD:THREAD "swank-indentation-cache-thread" RUNNING {1004AEFE23}>
 #<SB-THREAD:THREAD "finalizer" RUNNING {1001E30053}>
 #<SB-THREAD:THREAD "repl-thread" RUNNING {10020C1BC3}>
 #<SB-THREAD:THREAD "control-thread" RUNNING {1004AF2853}>)
 
 ;; find the thread by name and terminate it
CL-USER> (sb-thread:terminate-thread 
          (find "my main loop"  ;; using the thread name to find it
                (sb-thread:list-all-threads) 
                :test #'string= 
                :key (lambda (item) (sb-thread:thread-name item))))

Using Bordeaux threads

;; list all - mind the names
CL-USER> (bt:all-threads)
(#<SB-THREAD:THREAD "main thread" RUNNING {1001878913}>
 #<SB-THREAD:THREAD "auto-flush-thread" RUNNING {10020BFDB3}>
 #<SB-THREAD:THREAD "my main loop" RUNNING {100492B8E3}>   ;; this is the thread i suspect 
 #<SB-THREAD:THREAD "reader-thread" RUNNING {1004AEFD13}>
 #<SB-THREAD:THREAD "swank-indentation-cache-thread" RUNNING {1004AEFE23}>
 #<SB-THREAD:THREAD "finalizer" RUNNING {1001E30053}>
 #<SB-THREAD:THREAD "repl-thread" RUNNING {10020C1BC3}>
 #<SB-THREAD:THREAD "control-thread" RUNNING {1004AF2853}>)
 
 ;; find the thread by name and terminate it
CL-USER> (bt:destroy-thread
          (find "my main loop"  ;; using the thread name to find it
                (bt:all-threads) 
                :test #'string= 
                :key (lambda (item) (bt:thread-name item))))

Optimise the compilation for debugging

Either put (declaim (optimize (debug 3) (speed 0) (space 0))) at the top of every file you wish to be optimised for debugging or execute (proclaim '(optimize (speed 0) (space 0) (debug 3))) once which will affect all files.

Comments