Skip to content

Instantly share code, notes, and snippets.

@wsgac
Created May 14, 2026 15:15
Show Gist options
  • Select an option

  • Save wsgac/aef6eb2024a135987359a4672b446edb to your computer and use it in GitHub Desktop.

Select an option

Save wsgac/aef6eb2024a135987359a4672b446edb to your computer and use it in GitHub Desktop.
Simple example of using Green Threads to dispatch work and collect results
(ql:quickload :green-threads)
(cl-cont:defun/cc channel-squared (n h &key (threads 20))
(let ((in (make-instance 'gt:channel))
(out (make-instance 'gt:channel)))
;; Spawn workers
(loop
:repeat threads
:do (gt:with-green-thread
(loop
:for input := (gt:recv/cc in)
:until (eql input :done)
:do (gt:send/cc out (cons input (* input input))))))
;; Load numbers
(gt:with-green-thread
(loop
:for i :from 1 :to n
:do (gt:send/cc in i))
(loop
:repeat threads
:do (gt:send/cc in :done)))
;; Put them into H
(loop
:repeat n
:for (i . ii) := (gt:recv/cc out)
:do (setf (gethash i h) ii))))
(let ((h (make-hash-table :synchronized t)))
(gt:with-green-thread
(channel-squared 1000 h))
h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment