Acquire, Release (statements)

Syntax

Acquire Region$
Release
Region$

Description

Acquire a Critical Section. The script will wait until the region is available. Use this to provide synchronous access to data.

Release an acquired critical section.

A region can be acquired multiple times and must be released as many times as it is acquired.

Acquire and Release only work with the same process. In other words, two standalone executables cannot protect against each other using this mechanism.

Comments

 

Note: In the Basic Control Engine, when an event occurs, the script is started in parallel. If another event triggers the same script before the script ends, two scripts will be running in parallel. The Acquire and Release routines can be used to modify this behavior. Two options are available.

  1. Serialize the processing. In this case, the second instance of the script waits until the first is complete and then begins execution. This is accomplished by placing an acquire statement at the start of the script.

  2. Skip processing. In this case, the second instance of the script exits without performing any processing. The example in Acquire (FUNCTION) illustrated this.

 

Important: Be careful when acquiring more than one section (nesting), as deadlock can occur if two threads acquire the sections in different order. Consider the following:

 

Thread1

   Acquire "Section1"

   Acquire "Section2"

   ..

 

Thread2

   Acquire "Section2"

   Acquire "Section1"

 

In the above example, if Thread1 acquires Section1 and then Thread2 acquires Section2, both Thread1 and Thread2 will be blocked indefinitely.

 

Parameter

Description

 

Region$

String. A unique identifier of the region to be operated on.

Example

Consider the following example. Trigger is a point which caused the make decision to execute. The function may be called in response to two separate events with a different Point ID. The function will make a decision only if the time stamp of the point is more recent than the time the last decision was made.

 

Dim lastTime as Date

sub MakeDecision(trigger as Point, decision as Point)

   ' Only one thread may be within this loop.

   Acquire "MakeDecision"

   ' Make sure we release the "MakeDecision" section prior to leaving.

   ON ERROR GOTO RELEASEIT

   ' If we made a decision after this point changed then return

   if lastTime < trigger.TimeStamp then

      goto RELEASEIT

   end if

   lastTime = trigger.TimeStamp

   decision.Value = trigger.Value

   decision.Write

RELEASEIT:

    Release "MakeDecision"

    exit sub

end sub

More information

CIMPLICITY Extensions to Basic