[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lightweight coroutines in C (take 2)



I inadvertently managed to send this message to the list without a subject line,
and it's been brought to my attention that many people may have skipped over it
as spam. So here's a resend of the same thing - my apologies if you've already
read this...


I just came across this interesting little hack by Simon Tatham, which is used
to produce lightweight pseudo-coroutines in C:
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

The rationale given for developing this system is to improve the readability of
code that is naturally concurrent (the example given is a classic producer
consumer) without having to invoke the full weight of threads. This approach
even results in CSP-style blocking on communications between the routines :)

The implementation is fairly limited (and obviously it would easier to write the
target programs in occam, or to use CCSP ;) but it does provide an extremely
lightweight mechanism for writing fully portable pseudo-concurrent code in pure
C.

Adam Dunkels has apparently developed a lightweight "threads" library for use in
highly resource-constrained embedded systems, based on the same coroutine ideas
(http://www.sics.se/~adam/pt/index.html). Presumably a library of CSP-style
primitives would be fairly easy to implement as well.

Allan

-- 
Allan McInnes <amcinnes@xxxxxxxxxx>
PhD Candidate
Dept. of Electrical and Computer Engineering
Utah State University