Slashdot Mirror


Protothreads and Other Wicked C Tricks

lwb writes "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!"

6 of 229 comments (clear)

  1. Job security? by elgee · · Score: 4, Funny

    So this is so "counterintuitive" that no one else will ever understand your code?

    Sounds ideal!

  2. Seen this already by Anonymous Coward · · Score: 5, Funny

    I first came across this while I was working on the e-voting machines. There was a dept especially allocated to investigating how to hide certain features in c code to make them look like soemthing else.

  3. Re:Wait just a minute ... by LLuthor · · Score: 4, Funny

    And the JVM is written in C :)

    --
    LL
  4. Re:From the source: by Bastian · · Score: 4, Funny

    Wow. And I used to think C was frightening when I discovered the fun you can have with a program that takes command-line arguments when you start making recursive calls to main().

    When I saw that code snippet, I found myself switching back and forth between thinking "this is the most beautiful thing I have ever seen" and "dear god, who ordered that monster" so rapidly my brain almost a sploded.

  5. Re:Dijkstra says... by mikeage · · Score: 3, Funny

    Dijkstra is not $DEITY. There is a difference between a competent programmer and a brilliant programmer. Sometimes one has to be clever in order to get the job done.

    Actually, since the running of $export DEITY=Dijkstra, he is now.

    --
    -- Is "Sig" copyrighted by www.sig.com?
  6. Re:extremely limited applicability by gardyloo · · Score: 2, Funny

    Please note that this isn't interesting unless you work in, as, the FA says, a severely memory constrained system.

          Yeah, but my brain -- Ooh! Shiny!