Protecting Access to Global Variables

When a new thread is spawned, 4Test creates a new copy of all local variables and function arguments for it to use. However, all threads have equal access to global variables. To avoid a situation in which multiple threads modify a variable simultaneously, you must declare the variable as shareable. A shareable variable is available to only one thread at a time.

Instances where threads modify variables simultaneously generate unpredictable results. Errors of this kind are difficult to detect. Make variables shareable wherever the potential for conflict exists.

A declaration for a shareable variable has the following form:
[scope] share data-type name [= expr] {, name [= expr]}

At any point in the execution of a script, a shared variable can only be accessed from within the block of code that has explicitly been granted access to it. You request access to shareable variables by using the access statement.

An access statement has the following form:
access name1, name2, ...
  statement

where name1, name2, ... is a list of identifiers of optional length, each of which refers to a shareable variable and statement is the statement to be executed when access to the variables can be granted.

If no other thread currently has access to any of the shareable variables listed, 4Test executes the specified statement. Otherwise, 4Test blocks the thread where the access statement occurs until access can be granted to all the shareable variables listed. At that point, 4Test blocks competing threads and executes the blocked thread.

Example

share INTEGER iTestNum = 0      
public share STRING asWeekDay [7]
share ANYTYPE aWhoKnows

void IncrementTestNum ()
  access iTestNum
    iTestNum = iTestNum + 1