22 Dec 2002 MichaelCrawford   » (Master)

Threads vs. State Machines

I can see the advantages of using state machines over threads, but I don't see how one can get operating system support for distributing a program across all the processors in an SMP machine if you're using a state machine that has only one thread of execution. Perhaps someone can clarify this.

I realize threads are implemented in an OS kernel as state machines. What I'm asking about is how a user program can do SMP via state machines without the use of threads.

Saying to run two state machines in different processes isn't the right answer. That's the same as using two threads and presents all the same difficulties.

I agree that threads are resorted to too quickly by the lazy who don't treat them with the respect they deserve. Threads present a lot of problems and it takes care and skill to deal with them properly.

I wrote a test tool at Apple when I was one of the QA engineers for MacTCP, Apple's original TCP/IP network stack. Macintoshes didn't have threads or even proper preemptive multitasking. One could run interrupt tasks and do asynchronous I/O.

I implemented it as a state machine and it worked very nicely. Each Mac could have 64 TCP streams or UDP "pseudo-streams" and all the Macs on a network could communicate in any combination to create a pretty fierce amount of I/O. Without state machines that would not have been possible.

State machines tend to be much more conservative of resources than threads usually are. On the BeOS, each thread has a fixed stack allocation of (I think) 256 kb, and that is often very wasteful.

Hacker Angst

Sometimes when I have some particularly difficult work to do, particular when I have something totally new to work on, I just can't face it. The thought of even so much as opening any of my source files fills me with dread. It stops me cold.

Usually what I do is go to lie down for a while. Sometimes I take a nap. It's the worst feeling.

When my wife sees me like this she tries to encourage me to get back on the horse. It's pretty easy for her to tell that I'm in this state because I get up abruptly from my desk only to lay on the couch or bed.

Usually the best thing to do is try to find something easier to do to get myself back in the rhythm. Any work at all as long as it's somehow productive.

I hit this wall tonight with something difficult I planned to do. I have a programming task that won't be a lot of code but it's not obvious to me at all how to do it. After taking my usual time out I decided I would spend some time writing test code first and doing some testing. I probably won't even get to the difficult work tonight. It only matters that I get it done in the next week or so, so I should be able to work up the momentum.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!