Blocking and Non-Blocking code [solved]

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I haven't fully understand what are blocking and nonblocking code for embedded systems.

A common example of a blocking code is the delay function. It is said that delay codes block CPU. I don't understand how the delay code blocks the CPU as it executes the instruction to get specific delay. This means that cpu is not blocking it executing the instructions for delay. So why is it said that the codes block CPU in software delay?

I would like someone to clear my misunderstandings
 

click_here

Joined Sep 22, 2020
548
It's easier to show blocking code first...
C:
for(i = delayLoops; i != 0 ; i--)
{
    continue;
}
This is very self explanatory. The code decrements a variable until it is equal to 0, and then moves on. It is a delay.

However let's say that I have a few tasks to manage, non blocking code allows other things to happen.

This is a bit more tricky to show in a simple way - Imagine that I have 2 state machines running side by side in the same loop and I don't want one blocking the other - I would make non-blocking code...
C:
for(;;)
{
    switch(stateMachine1)
    {
        case state1:
            counter1 = 100;
            stateMachine1 = state2;
            break;

        case state2:
            counter1--;
            if(counter1 == 0)
            {
                stateMachine1 = state1;
            }
            break;
    }

    switch(stateMachine2)
    {
        case state1:
            counter2 = 200;
            stateMachine2 = state2;
            break;

        case state2:
            counter2--;
            if(counter2 == 0)
            {
                stateMachine2 = state1;
            }
            break;
    }
}
Note that instead of using a for loop I decremented my variable and went and did something else


Does that make sense?
 

Ian Rogers

Joined Dec 12, 2012
1,136
Blocking just means that the CPU cannot do anything else while the blocking code is in action..

the delay routine is the prime example... As click_here said there are other examples...
 

ericgibbs

Joined Jan 29, 2010
18,766
hi P,
Consider you have program that tests for say a key press, then takes some action.

While in a Delay loop the key press cannot be detected, so the delay is effectively blocking the reading of the key press pin.
E
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I presume the switch is pulled up and read logic 1 when open and logic 0 when pressed.

C:
#define Switch_Not_Pressed  0
#define Switch_Pressed      1

void main ()
{
    Switch_State = Switch_Not_Pressed;
  
    if(Switch == 0)                       /*Switch is pressed */
     {
        Debounce_time(50);               /* wait 50 msec */         
       if(Switch == 0)                    /* check switch again */
            Switch_State = Switch_Pressed;   /* switch is  pressed */

     }
}
CPU execute delay instruction for 50ms. Is there anything else that can be done in 50ms that's we call Non-Blocking code
 

click_here

Joined Sep 22, 2020
548
If you want non blocking delay for debouncing you could make another state for it, and set it up like the second example I had above.

Using that switch/state method with non-blocking code is a powerful way for a beginer to introduce multitasking in a bare metal program.
 

MrChips

Joined Oct 2, 2009
30,714
Blocking example
start task
wait until task is done

Non-blocking example
start task
repeat - do something else - until task is done
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
non blocking code allows other things to happen.
What does it means "non blocking code allows other things to happen". Can you explain with example because previous example is little bit hard to understand

Blocking just means that the CPU cannot do anything else while the blocking code is in action..
What does it means that "CPU cannot do anything else while the blocking code is in action"' need clarification with example

Blocking example
start task
wait until task is done

Non-blocking example
start task
repeat - do something else - until task is done
Looking for example
 

MrChips

Joined Oct 2, 2009
30,714
Blocking example
ask user to hit a key
repeat - is key switch pressed? - until key switch is pressed

Non-blocking example
ask user to hit a key
repeat - if key switch is not pressed, flash LED - until key switch is pressed
 

MrChips

Joined Oct 2, 2009
30,714
Blocking example
put kettle on stove, turn on stove
wait until water boils
turn off stove

Non-blocking example
put kettle on stove, turn on stove
repeat
- - watch the news
- - listen to the radio
- - check email
until water boils
turn off stove
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Non-blocking example
ask user to hit a key
repeat - if key switch is not pressed, flash LED - until key switch is pressed
If I'm understanding your example correctly.The cpu first detects whether the switch has been pressed or not, if it is pressed first time, then the CPU doesn't waste 50 ms of time, it flash the LED for 50 ms. After that again the CPU detects whether the switch is pressed or not pressed. If it's pressed then it's stable. Instead of delay time for switch, we are doing some more useful task LED flashing

Non-blocking code means that if our program has a delay time then we can do some useful work in that delay time
 

MrChips

Joined Oct 2, 2009
30,714
Software delays are generally blocking mechanisms, depending on how the delay function is implemented.
50ms is a lot of CPU time to waste in computer world.

While flashing an LED, you want any delay to be non-blocking. Timer delay functions would be best implemented with HW interrupts if you desire non-blocking operation.
 
Top