programmer things

Concurrency in Common Lisp With ChanL

Aug26th, 201212:00 am

Rob Pike did a great talk
at Google on concurrency in Newsqueak. The gist of it is that you spawn
concurrent tasks in separate lightweight processes and synchronize by
communicating through channels. That stuff worked its way into the
programming language Go. After seeing that video I wanted to try it out
myself in Common Lisp, of course. It turns out there is a
Quicklisp-installable library called
ChanL that provides just that
functionality. I don’t know how lightweight the threads are, but the
interface is similar.

A simple, but effective example of using channels is to perform a number
of IO-bound tasks in parallel, such as retrieving several web pages.
It’s surprisingly simple to do in parallel. To start, I’ll define a
little timing macro:

It takes over 2 seconds to make all these requests in serial fashion.
There may be a way to queue up these requests in parallel with the
Drakma library, but instead we’ll just use the ChanL library. Let’s
define a function, like DO-REQUEST that instead of printing its progress
will send messages over a channel.

Here, the whole operation takes only as long as the longest single
request and the overhead of spawning separate threads is negligible
compared to the time saved by making these requests concurrently. It is
important to note that, by default, the channels are not buffered so
calls to SEND will block until there’s a RECV called on the same channel
in a different thread. ChanL also provides buffered channels and some
other goodies which I haven’t touched on here.