Jump to content

Avoiding Race Conditions


matapan

Recommended Posts

Posted

What are some of the best programming practices to avoid race conditions?

 

You know that there is always someone in your circle of family and friends who are button pushers. The ones that like to turn things on and off, on and off in rapid succession before they ask: "What does this do?"

 

My simple, sequential programs for turning things on in delayed sequence go to h*ll when this happens.

 

What's a straightforward way to prevent race conditions from occurring, allowing the first press to complete before listening to any other events? Are events pushed into a queue? Or, maybe it's better to stop running a program associated with an event if a newer event supercedes it. Is that safer, especially when dealing with electrical switches?

Posted

Hi matapan,

 

Good question!

 

All events go to a queue and are processed sequentially in the order they are received. So, the best thing would be to break up any OR conditions into new programs, check for exception, and stop those offending programs in the face of exceptions.

 

In the case of ANDed conditions, I do not think you will have any problems since as soon as one of the conditions is false, the whole program stops.

 

With kind regards,

Michel

 

What are some of the best programming practices to avoid race conditions?

 

You know that there is always someone in your circle of family and friends who are button pushers. The ones that like to turn things on and off, on and off in rapid succession before they ask: "What does this do?"

 

My simple, sequential programs for turning things on in delayed sequence go to h*ll when this happens.

 

What's a straightforward way to prevent race conditions from occurring, allowing the first press to complete before listening to any other events? Are events pushed into a queue? Or, maybe it's better to stop running a program associated with an event if a newer event supercedes it. Is that safer, especially when dealing with electrical switches?

  • 2 weeks later...
Posted

Hi,

 

an easy way would be to create a flag and use it as a condition for your programs.

 

First, create an empty program

Program name : Ready to run

If (No condition)
Then

Else

 

Next, create a serie of triggers for your action and add your flag as a condition

Program name : Trigger 1

If   ( Switch_No1 is turned On
       Or KeypadLinc B is turned On )
   And Ready to run is True

Then
  Run program Action_1 Then part
  Run program Ready to run Else part

Else

 

Be sure to turn off the flag only at the very end of this program. Doing it at the beginning would make the program false and stop it. Another option would be to turn off the flag as the first action of the Action_1 program.

 

Finally, define what you wish to do in the Action_1 program before resetting the flag at the end.

Program name : Action_1

If (No condition)

Then
       Insteon Set Scene A On
       Wait 5 minutes
       Run program Ready to run Then part

Else

 

This way, no triggers will start when one is running and because Action programs are only started by those triggers, no action will be taken by the system.

 

Have fun with your programs,

 

Jacques

Posted

Please forgive the intrusion but I have a couple of questions. Isn’t the natural state of an empty program false after creation? Do you have another program which puts the flag true in order for the main program to run? Also after a power failure is there a program to return the flag to true?

 

Could you set up the main program to look for a false, then during the main programs running the flag was turned true, then after execution it was turned false again?

 

Thanks,

Tim

Posted

Hi,

 

By default, programs are true, not false.

 

As you suggested, it is better to explicit a status than letting it by default. You can easily create a program that will force the status either at boot up of the ISY or once per night.

 

Here, I have one that check the status of a day : every week days, it checks if it is an holyday or vacation. If it is, it does not wake me up. If it is a business day, it turns my lights On smoothly.

 

It would also be easy to reverse the logic : set your flag as "Already running" instead "Ready to run" and reverse the entire logic. It would gives you the same result (if trigger and not already running ; then...)

 

Jacques

Posted

Hi JacquesB,

 

I am sorry, I must be missing something here.

 

By default, programs are true, not false.

 

I created an empty program and saved it. When I check on its status it shows as false. So in your example program the main program would never run, until the flag is set true. Is there another program not shown that is used to initially turn the flag true?

Posted

Hi,

 

You are right. I tried it and the newly created program has been set to false. I was sure they were true by default... Sorry about that.

 

So, either you reverse the logic (the variable becomes "Already running" instead of "Ready to Run"), or create a reset program for turning that program True at boot up and whenever you wish.

 

Jacques

Guest
This topic is now closed to further replies.

×
×
  • Create New...