Task reentrance

  • Hello.

    I have just started programming on AS.


    For example I want to create wrapper subroutines for PRINT instruction to add some additional info to log output:

    Code
    .PROGRAM LOG.pc (.$msg)
        PRINT $TIME + ": " + .$msg
    .END


    then I call it from two background programs:


    each program1.pc and program2.pc calls LOG.pc in infinite loops:


    When I start main.pc I got an error: (P1014) Cannot execute because program already in use. No = 1002.

    But why? LOG.pc does not use any global variables

  • It seems logical to me that this error occurs.

    How can the same program be called at the same time by a different program.

    When calling a subroutine you are placing it in the execution stack, so therefore it will be in use.

    I am pretty sure no 'caching' or 'prefetching' occurs with PC Programs as there are no motion instructions allowed.

    PC programs executed (including calls) are asynchronous.


    Personally with a simple print instruction like that, replace the call, with the print command and it'll more than likely work fine, but as you are waiting 0.5s per cycle, I would expect you to probably see more than one consecutive print instruction for each program.

    ie

    Time + program1

    Time + program1

    Time + program2

    Time + program2


    Not too sure what your intentions are with this?

  • It's a pity that it is impossible to move common parts of code to subroutines and call them independently

    Yes, you can by using the parse parameter feature in step 0 as you're doing.

    What you can't do is simultaneously call the same program.

    Variables/values which are parsed in, get grabbed and stored locally at the start of execution.


    What you are doing fails at the start because they are starting together and stops one of the PC tasks.

    With your auto restart routine every 5 secs, then starts it back up and as long as it starts it back up when the other task is not using the subroutine, it'll run as they are executing at different times now.


    An alternative (haven't tried it, something for you to try).

    PCSCAN instruction.

    You can set the PC Scan time using this, so logically thinking, you should be able to introduce an offset between the PC Tasks......not sure whether this would be reliable enough, but worth a shot

Advertising from our partners