Fri, 07 Oct 2005

Protothreads and Other Wicked C Tricks

I've always been fascinated by code along these lines. We used similar tricks in the early 90's to force Windows 3.1 to pre-emptively multitask with background DOS processes. It used to amaze people at trade shows that we could kill their voice processes by starting the paint program and holding down the mouse button, while ours kept right on answering the phone (and allowing paint to draw). Seems trivial in this day and age of XP, the Internet and Unix based systems, but back then, well, that was some trick!

Interesting to note that embedded systems with tiny memory footprints can still used this kind of magic efficiently.

For those of you interested in interesting hard-core C programming tricks: Adam Dunkels' protothreads library implements an unusually lightweight type of threads. Protothreads are not real threads, but rather something in between an event-driven state machine and regular threads. But they are implemented in 100% portable ANSI C and with an interesting but quite unintuitive use of the switch/case construct. The same trick has previously been used by Simon Tatham to implement coroutines in C. The trick was originally invented by Tom Duff and dubbed Duff's device. You either love it or you hate it!

(link) [Slashdot]

/Technology | 0 writebacks | permanent link


comment...

 
Notes: If you put a <mailto:> link in the URL field your address will not be mangled: this could be a bad idea as your email address could be easily harvested by bots designed for SPAM. The comments field should now format correctly for line feeds and carriage returns: when you hit the 'Enter' or 'Return' keys in your comment it should break to a new line. The text should wrap cleanly. Please let me know if it doesn't. No HTML tags will pass through - entering links seems to be the main cause of comment SPAM. Also, please be sure that Javascript is enabled in your browser before attempting to post a writeback. Sorry for any inconvenience, but this really helps cut down on the amount of comment SPAM I have to deal with.
 
 Name:
 URL:(optional)
 Title: (optional)
 Comments:  
Save my Name and URL/Email for next time