lightweight threads in Lisp
Peter Van Roy
peter.vanroy at uclouvain.be
Wed Mar 12 16:24:05 CET 2008
Camilo Rueda wrote:
> Dear Peter,
> One of our students is doing a "stage" at Ircam. He is studying ways of
> implementing lightweight threads in Lisp. What would be a good source of
> information on this? what is your perception on the difficulty of doing
> this?
> Thanks for your help!
>
> regards,
> Camilo
>
Good question! I already explained to him some approaches he could
follow, but
he has some gaps in his education on what threads are and how they execute.
If you want the threads to be efficient (really lightweight) then the
Lisp implementation
has to give some support (unless you want to change the run-time system
and compiler).
At least, does it have cheap coroutines or something like setjmp/longjmp.
If the Lisp gives no support then you can still get lightweight threads
if you change
the way you write programs. Each program or part that you want to run
inside a
thread should be written as an event loop:
while (true) do
event e1, e2;
e1:=read_event(); /* read from event queue */
e2:=do_work(e1);
post_event(e2); /* put in event queue */
end
The thread scheduler will manage how all these programs execute
concurrently.
Basically, the thread scheduler picks an event e and executes the
appropriate event
loop for one iteration. Then it picks another event, etc. That is how
you implement
the interleaving semantics of threads.
PS, the E system (www.erights.org) is written using this execution
model, and I
believe you can get the source code from their Web site.
So, the conclusion is that if you are willing to change a little how you
write programs,
then it's not too hard to write lightweight threads. If you have big
chunks of legacy
code then it's harder.
Peter
More information about the mozart-hackers
mailing list