Shared Variables
Shared variables are used to save the state, either at the module level or for the duration of a job.
Using shared variables, you can share data across process instances associated with a module or a job. A process instance can read or update the data stored in a shared variable. The shared variable data updated by one process instance is accessible to other process instances of a Module or Job.
There are two types of shared variables: module shared variables and job shared variables. Both module and job shared variables are defined at the module level and can be accessed in a process using the activities Set Shared Variable and Get Shared Variable. Refer to Using Shared Variables in the Application Development guide for details on how to define and use shared variables.
Module Shared Variables
Module shared variables are used to share the state at a module level and are visible to all process instances created from the processes that are within a module. Module shared variables can be read and updated by the process instances during execution. Once the value is updated, the new value is available to all the process instances created from the processes that are within the module. Consider an example where the exchange rates are updated daily and the updated exchange rates should be accessible to all processes in a module. You can create a module shared variable to hold the exchange rate and use one process in the module for updating the exchange rate. All other processes in the module that require the exchange rate can retrieve the current value through the module shared variable.
Job Shared Variables
Job shared variables are used to share the state at the job level for the duration of a job. A copy of the job shared variable is created for each new job and it is accessible to all process instances associated with the job. Job shared variables can be used to share data between all process instances of a job without creating an input or output schema for the called process.
Sharing Job Shared Variables Between Inline and Non-Inline Processes
Inline subprocess are executed as part of the caller (parent) process job and hence the current value of the job shared variable is passed from the caller process to the inline subprocess. Non-inline subprocesses spawn a new thread and are not executed on the same job as the caller process. Hence a non-inline subprocess obtains a copy of the job shared variable and does not obtain the current value of the job shared variable from the caller process.
At runtime, the engine allocates a new job shared variable for each new job and the value of that variable is visible only to that job.
Shared Variable Synchronization
Multiple process instances can potentially access or update a shared variable at the same time. For example, a module shared variable can be accessed by multiple jobs concurrently. Without a synchronization mechanism, a process instance could update the value of a shared variable while another process instance is trying to read the value. This could result in an unpredictable value for the shared variable.
Critical Section groups can be used to synchronize access to shared variables. A Critical Section group allows only one process instance to execute the Critical Section group and its contents at a given time. To synchronize shared variables, use a Critical Section group to contain the activities that access the shared variables (Set Shared Variable and Get Shared Variable). Once a process instance begins executing a Critical Section group, other concurrently running process instances that are associated with that Critical Section group wait at the start of the group until the currently running process instance exits the critical section group. This ensures that the value of the shared variable is not modified while another process instance is accessing it. To synchronize multiple critical section groups, use a shared lock. The shared lock can be defined using a module or a job shared variable.