Pass by reference example

A Score Level 1

Can someone explain me any scenario where we can use pass by reference under subprocess ? Is pass by reference valid only for asynchronous type sub process?

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    The easiest example I can think of is, in the parent process you have a boolean PV value (initialized to FALSE) which would indicate when processing within a subprocess has completed (or passed a certain point).  You pass this boolean value by reference into the subprocess, and when the subprocess sets the value to TRUE instead, the parent process's PV value will also be updated - this could be used to drive a rule node to move some other process forward in the parent process.

    Passing by reference is definitely valid for synchronous processes most commonly, though apparently it can also be used for asynchronous process calls (i forget whether i've tried this myself).  Also per the documentation, note that values can't be passed by reference from the Start Process node (there aren't even any apparent configuration options close to it).

  • Honestly I don't see pass by reference used very often with asynchronous processes. The issue is that the processes are entirely independent, so your parent process could complete before the child process updates the variable (or vice versa). If you plan on waiting for a variable value to change in your process, it's probably better to just use a synchronous sub-process.

    Do you have a use case in mind where you are planning to use pass by reference?

  • 0
    Certified Lead Developer
    in reply to Peter Lewis
    I don't see pass by reference used very often with asynchronous processes.

    Same here - though assuming it actually works, I could see certain use cases where the parent process (assuming it's still running at any given point) could benefit from a bit of visibility into what's currently happening in an async subprocess.  I forget if I've ever actually done this, though.

  • 0
    Certified Lead Developer

    I typically avoid "byReference" where possible.  On the majority of the projects I was on, it simply wasn't used.  The standard for those projects was to explicitly make the same variable an output of the subprocess.  This is good because it gives you very tight control over what gets it's value updated, and you can read the configuration of the subprocess node to figure out exactly what gets changed without having to scour the subprocess model to find it.

    I can think of a few instances where you'd want to pass by reference.  Imagine you had 2 simultaneous workflows.  You could have one in a subprocess and pass a "status" variable by reference.  You could park the parallel flow behind an AND gate waiting for a trigger by the referenced variable being changed.  That way you know the subprocess has gotten at least this far.  And you don't have to go through all the effort to build infrastructure for sending message from one PM to another, which I also hear is frightfully slow.  Each process could update the same status variable, and now the two could run in parallel while holding themselves back to avoid race conditions with each other.  I'm actually thinking I'd like to build a POC of that very thing now.

  • 0
    A Score Level 1
    in reply to Mike Schmitt

    We can get the subprocess value from output variables also right , then whats the use of passing a variable by reference in  synchronous mode ?

  • 0
    Certified Lead Developer
    in reply to ST07

    That is a very good question, isn't it?

    Perhaps the only advantage is that you could have a second process thread that can start on the main process while the synchronous process is still running.  You'd have that thread triggered by the referenced variable changing.  Theoretically that could do something, but I don't think I've ever seen it done.

    There's usually never any value add to passing by reference.

  • Typically, you don't do it. The only exception I've ever used it for was to be able to run a synchronous sub-process and, whilst that was running. continue processing in the parent process, and then using a byRef Boolean to synchronise the two threads of processing.