Worker Pools in Lisp
In this example, we’ll look at how to implement a worker pool using threads and channels in Common Lisp.
Here’s the worker function, of which we’ll run several concurrent instances. These workers will receive work on the jobs
channel and send the corresponding results on results
. We’ll sleep a second per job to simulate an expensive task.
We use the bordeaux-threads
library for thread management and cl-chan
for channel operations, which provide similar functionality to Go’s goroutines and channels.
In the main
function:
We create two channels:
jobs
andresults
, both with a buffer size equal to the number of jobs.We start 3 worker threads using
make-thread
. Each worker runs independently and processes jobs from thejobs
channel.We send 5 jobs to the
jobs
channel and then close it to indicate that’s all the work we have.Finally, we collect all the results of the work from the
results
channel. This also ensures that the worker threads have finished.
When you run this program, it will show the 5 jobs being executed by various workers. The program should take about 2 seconds to complete despite doing about 5 seconds of total work because there are 3 workers operating concurrently.
Note that Common Lisp doesn’t have built-in constructs exactly equivalent to Go’s goroutines and channels. We’re using threads and a channel library to achieve similar functionality. The exact behavior and performance characteristics might differ from the Go version.