![]() |
State Machines
Okay, how many of you guys have heard of state machines?
State machines are functions that perform a small amount of a task each time they are called and keep track of their position in the process using a state variable. State machines are used for multi-tasking; a main loop can call several state machines each time through so each function gets time to perform its duties. Maybe I can even find time to come up with an example and an excercise if people are interested --WetWired |
what r u talking about?
|
Quote:
If I somehow find time between working on my phpBB style, setting up phpBB on my Linux box, and creating my new website design, I might come up with a simple example program --WetWired |
Never heard of 'em. :confused:
|
Something like, there's a main function... and 3 sub functions...
Main SmallFunc MedFunc LargeFunc And then, Main calls each one, but each one only goes a certain ways through.. SmallFunc, a few commands MedFunc, a few commands more than SmallFunc LargeFunc, a few commands more than MedFunc So that, all of the functions complete at about the same time? Is that what you mean? |
Essentially, that's the way the program appears to work to the user, but how it works is each function has a state variable and a switch statement, so the first time it's called it might do the code for state 0, then set the variable to state 1, next time it's called it'll do the code for state 1, then it might set the state to 50 to do that code next time, or stay in state 1 while waiting for something. All the functions are called in a loop in the main function and each does a little part of what they do each time they are called so it appears as if they are running at the same time.
It's a simple concept, though hard to explain on a msg board... --WetWired |
Okay, I know what you're talking about now...
switch (melmac) { case 0: {run code here; melmac = 1; break;} case 1: {run code here; do calculations; if (bob == 5) melmac = 2; else melmac = 3; break;} case 2: {cout << "Bob equaled five!"; break;} case 3: {cout << "Stupid Bob, it didn't equal five!"; break;} } Will only run one case statement each time through (might be a bit off, haven't programmed in C++ in a few months...) |
nope never heard of it
|
but they seem pretty useful...
|
never heard of :(
|
All times are GMT -6. The time now is 07:28 PM. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.