Jump to content

Programming: Are programs "single-threaded"?


gduprey

Recommended Posts

Howdy,

 

As part of my ongoing attempt to get clarity around fuzzier (to me) aspects of ISY programming, I'm wondering what happens when a program as is run as a result of an event (i.e. Control event) and while the program is executing, the event occurs again?  Will a second version of the program be run concurrently?

Will the event (from the standpoint of the program) be ignored?
 

Consider

New Program - [ID 0013][Parent 0001]


If
        Control 'Office Overhead Lights' is switched On


Then
        $Occupied += 1
        Wait  10 minutes 
        Run Program 'Doors Locked' (If)


Else
   - No Actions - (To add one, press 'Action')

If the trigger (Office Overhead Lights switched on) occurs and the program runs, during the 10 minute delay, if the office lights are turned on again, what happens?  Does a 2nd instance of the same program start running?  Does the current running program get stopped and restarted?  Is the Control "event" ignored?  Or might it be queued up to run after this current "instance" is complete?

Thanks!

Gerry

 

Link to comment

Hmm -- if that is the case, how would you write a program where, after an event occurs, you want to insure it doesn't happen again for some period of time?

For example, say you have some sort of pump or compressor that you are looking to avoid short-cycling.  So when the device is turned on, you want to insure that device stays on for XX minutes.  If the above answer is that case and a repeat of the triggering event occurred, depending on the program, it might run a timer again or do other actions that should not have happened? (note: this is just an imperfect example -- the basic is how to insure a program runs to completion if the above reply is how things are handled)?

 

So again, if the above is true (anytime an IF statement trigger/statement becomes true, any current running version of the program is stopped and restarted/re-evaluated), is there any way to have a WAIT statement that will NOT be so interrupted?

Gerry

Link to comment

Another option is to have a program that is "True" if the program you don't want to re-run has been run recently, and check the program status as part of your conditions:

Dehumidify ran recently - [ID 00A8][Parent 00A1]

If
        From    Last Run Time for 'Dehumidify'
        To      Last Run Time for 'Dehumidify' +  1 hours  (same day)
 
Then
        Wait  1 second
 
Else
   - No Actions - (To add one, press 'Action')

 Program logic to not run again if Dehumidify ran recently looks like this:

Dehumidify - [ID 00A7][Parent 00A1]
If

        Program 'Dehumidify ran recently' is False
...
Edited by KeviNH
Link to comment

Gerry,

 

Please note that it is only programs containing "wait" and "repeat" statements that would be interrupted.

 

In no case, as I understand, would you have more than one case of a program running at any given time. If a program is retriggered, xurrent execution would halt.

Link to comment

Gerry,

 

Please note that it is only programs containing "wait" and "repeat" statements that would be interrupted.

 

In no case, as I understand, would you have more than one case of a program running at any given time. If a program is retriggered, xurrent execution would halt.

What he said.

 

The Then and Else clause are referred to as "atomic", meaning not separable (like in the old days before we understand nuclear fission), EXCEPT when a wait or repeat is encountered.  These two commands separate the then/else clause into multiple atomic units.  

 

So, in short, once an atomic section of a then/else starts, all items will be executed.  If during the micro-second of execution, another trigger occurs, the program will still finish the current atomic unit, and then start over.

 

For example

 

Then

Repeat every 10 minutes

do thing 1

do thing 2

do thing 3

wait 5 minutes

do thing 4

do thing 5 

do thing 6

 

 

The above has two atomic units.  things 1,2, and 3 is the first, things 4,5,and 6 is the second.  The thing 1,2 and 3 essentially happen instantly, but at the micro-second level, they may occur in any order.  Regardless, all three things will happen once that section starts.  If, in the midst of things 1,2,3 being executed (during that micro-second), a new trigger happens, still all 3 things finish, however, the "wait" will be a terminating point for this execution of the program (it starts fresh on the new trigger).  Or, the repeat would also be a terminating point if the trigger event happened during/after things 4,5,6 and before things 1,2,3 repeated.

 

Keep in mind, that atomic sections may execute in any order.  So if your thing 1,2,3 are math functions where the result of 1 is passed to 2 and then 3, you might not get the answer you expect.  Although typically they happen in order, it is not a 100% thing.  If math function 1 is way more complex than math function 2, then 2 may execute before 1 finishes.  

Edited by apostolakisl
Link to comment

This does help -- I appreciate all the comments.

 

One last one -- if I have "program A" running (with some WAIT statement in it) and then another program attempts to run "program A" as well, will it stop the program and restart it?  I get that the conditions for an If of an enabled program will stop the WAIT and restart it, but wanted to confirm that simply running the same program (perhaps running the Then) will also do that (my guess based on comments so far is Yes).

 

Gerry

Link to comment

This does help -- I appreciate all the comments.

 

One last one -- if I have "program A" running (with some WAIT statement in it) and then another program attempts to run "program A" as well, will it stop the program and restart it?  I get that the conditions for an If of an enabled program will stop the WAIT and restart it, but wanted to confirm that simply running the same program (perhaps running the Then) will also do that (my guess based on comments so far is Yes).

 

Gerry

Your guess is correct.  Another program calling this program is a trigger event and will reset the program.  You will never have multiple simultaneous running "then" or "else" executions.  

  • Like 1
Link to comment

Another way to think about it.

 

Once triggered, a programs 'Then' and 'Else' clauses run 'atomically' - they will complete without interruption - EXCEPT when 'Wait' or 'Repeat' is encountered. If the triggering condition has changed when 'Wait' or 'Repeat' is encountered then the run is aborted and 'If' is reevaluated.

 

So, if you have a program that triggers on a state variable being 1, and in the 'Then' you set the state variable to 2 in the first command and 3 in the second.

 

If you then manually set the state variable to 1 it will result in it being set to 3 - since without a wait the trigger will not be rerun on the first step.

 

If you insert a wait between the two set variable commands - the 'Then' will trigger and change the variable to 2, the wait will be encountered allowing the 'If' clause to be evaluated again. If is reevaluated (now being false) WI run 'Else'. Nothing there so the variable ended up at 2.

 

Sent from my Nexus 6P using Tapatalk

Edited by MWareman
Link to comment
Guest
This topic is now closed to further replies.

×
×
  • Create New...