From: Patrick Li Date: 2008-08-23T04:19:16+09:00 Subject: Re: Saving the state of a Thread? Thanks Robert for the input. Yes I agree. That's a very clean and flexible approach. My code is currently structured using state machines, which is similar in concept to your operation stages. This sort of programming is actually one of my primary motivators for thinking about a thread-based paradigm instead. Here's my original idea, in it's original un-condensed and unabbreviated mental form. Maybe you have some ingenious ideas: Currently, a real-time simulation loop looks something like this: while true scenario.update (secondsSinceLastIteration) scenario.draw end And your scenario code would like something like this (ie. for a traveling ball): def update (timePassed) @position += timePassed*velocity end You would agree that this is all very straightforward. But what if you want the ball to travel back and forth 3 times? You'd have to do something like this: def update (timePassed) if @numberOfBackAndForths == 3 return if @travelingForward @position += timePassed*velocity @travelingForward = false if (reached end) else @position -= timePassed*velocity if reached-end @travelingForward = false @numberOfBackAndForths += 1 end end end So can see that @travelingForward essentially describes the state of the system. This sort of manual state bookkeeping can get really out-of-hand pretty quickly. BASIC IDEA: Wouldn't it be nice to be able to write something like this? def start 3.times do #TRAVEL FORWARD update(5 seconds) do |timePassed| @position += timePassed*velocity end #TRAVEL BACKWARD update(5 seconds) do |timePassed| @position -= timePassed*velocity end end end Ahh... so much cleaner isn't it? This is a shift to a thread-based paradigm. So far, I have everything working, EXCEPT when the simulation needs to be saved to disk. I can save @position, and I can save how many iterations are left. But after I resume the simulation, how do I get the thread to start from the appropriate line of code? I never thought I'd ask for it, but a goto statement sounds like a blessing right now. Thanks for all your replies. -Patrick -- Posted via http://www.ruby-forum.com/.