What is a state machine?

Asked

Viewed 2,425 times

13

I’m doing a tour on the site, researching on asynchrony, threads, parallelism and the like.

Upon finding this reply, I noticed that the author makes a quote about state machines.

I didn’t understand very well what a state machine would be and what relationship it would have with asynchrony, threads and the like.

  • What would be state machine?

  • What is the relationship with the above terms?

2 answers

10


Asymchronism can be obtained through a state machine since it just needs to ensure that no waiting occurs while it is doing something potentially time consuming, so it needs to exchange the execution context between more than one part of the application.

This is an old and well-known technique used in many problems. She will exchange one or more certain states according to what goes on in something related to what she is controlling.

Even this mechanism has several ways to implement it. The way the input that causes state change can be obtained in several ways. An event system indicating the change of state is quite common.

The image there on the question is very illustrative how complicated it is to control the flow of execution. Follow the Indian war.

Máquina de estados async/await

Wikipedia article.

An implementation in C#.

Detailed explanation of how async/await c# works. There are more or less examples of how code actually looks when this mechanism is used. Everything there is a state machine, and what is waiting is responsible for changing the state of that machine. When there is no more expect a different processing runs by terminating what has started, but does not block the execution of other things. There is a control whether you can still perform something else, or whether you should resume processing what is on hold.

A example of actual code that is generated by the compiler. Simple, no?

I’m still gonna get better.

  • I know it’ll get better yet, but I’ll give my +1 of confidence

  • 2

    Hoping for improvements!

  • :P :P :P :P :P :P

6

A state machine is a computer science term describing a computer program. If you think about it, a system is a set of states, for example, in the sequence of Fibonacci we have the sum of the last two numbers to form the next number. This machine would have an initial state:

N1: 1
N2: 1
N3: 2

The next state would be:

N1: 1
N2: 2
N3: 3

And so on and so forth. A state machine is the graphical representation of a computer program and all its possible states and is usually used to illustrate data and information flows, describing the states that happen in the middle of the way.

It has no relation to the terms, because it is only used for illustration and diagramming. It may be that the person has used of a state machine to represent the operation of some asynchronous program or some thread.

Behold this link for a better reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.