From: John Carter Date: 2006-09-12T09:00:59+09:00 Subject: Re: state machine in ruby On Mon, 11 Sep 2006, Paul Lutus wrote: > John Carter wrote: > >> First off I would just like to note a small mathematical congruence.... >> >> Every state machine can be replaced by a Thread. ie. Instead of a tangly >> state machine, you have a thread sucking on an event queue. > > Let me add my 2c. Every program is a state machine. The difference between > an ordinary program and what we might call a "state machine" is one of > explicit expression. Correct. > Also, the use of threads make the state of the state machine harder to > determine. By using threads, the thread scheduler's quirks, and every other > thread being run, becomes part of the behavior of a particular thread's > state machine. One might go so far as to say that threads that show any > interaction at all prevent a state machine from being deterministic. Yes and no. If you only ever communicate event's with between thread's via a SizedQueue's of length 1, you have the same "interlocking cog" determinancy as an unthreaded state machine only model. The difference is you may ... * Program each Thread/state machine in a "simple forward manner" same as any program * You can, in appropriate cases, get substantial performance gains by increasing the Queue sizes. Djikstra's "Goto Consider Harmful" is even more applicable to state machines where _every_ transition is a "goto"! Where there is more than one state machine, your main program becomes a poorly designed inefficient home grown scheduler. The fact is state machines suffer the same defects as Threads, ie. races and deadlocks, it's just the "interlocking gears" nature tends to make them trigger or not trigger the defects the same way on every run. (However, change the program and that is not true.) John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand "We have more to fear from The Bungling of the Incompetent Than from the Machinations of the Wicked." (source unknown)