What is a state machine?

9

I'm doing a site tour, researching on asynchronism, threads, parallelism, and the like.

When I found this answer , I noticed that the author makes a citation regarding state machines .

I did not quite understand what a state machine would look like and what relationship it would have with asynchronism, threads, and the like.

  • What would be a state machine?

  • What is the relationship with the above terms?

asked by anonymous 29.05.2017 / 14:12

2 answers

6

Asynchronism can be obtained through a state machine since it only needs to ensure that there is no waiting while doing something potentially time-consuming, so it needs to switch the execution context between more than a part of the application.

This is an old and well-known technique used in various problems. It changes one or more certain states according to what is happening in something related to what it 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 that indicates the change of state is very common.

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

Wikipedia article .

A C # implementation .

Detailed explanation of how C # async/await works . There are more or less examples of how the 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 waiting a different processing is executed closing what started, but does not block the execution of other things. There is a control if you can still do something else, or if you should resume processing what is on hold.

An actual code example that is generated by the compiler . Simple, is not it?

  

I'm still going to get better.

    
29.05.2017 / 15:07
5

A state machine is a computer science term that describes a computer program. If you stop to think, a system is a set of states, for example, in the fibonacci sequence 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. A state machine is the graphical representation of a computer program and all its possible states and is generally used to illustrate flows of data and information, describing the states that happen in the middle of the path.

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

See this link for a better reference.

    
29.05.2017 / 14:58