background image
<< Client/Server Testing - Alternative machine handle operator | Client/Server Testing - The Acquire function >>
Client/Server Testing - Synchronizing threads with semaphores
<< Client/Server Testing - Alternative machine handle operator | Client/Server Testing - The Acquire function >>
370
User's Guide
21 I
MPLEMENTING
C
LIENT
/S
ERVER
T
ESTING
Using 4Test's parallel processing features
A declaration for a shareable variable has the form:
[
scope
] share
data-type name
[=
expr
] {,
name
[=
expr
]}
·
scope can be either public or private. If omitted, the default is public.
·
data-type is a standard or user-defined data type.
·
name is the identifier that refers to the shareable variable.
·
expr is an expression that evaluates to the initial value you want to give
the variable. The value must have the same type you gave the variable. If
you try to use a variable before its value is set, 4Test raises an exception.
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 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(s) 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(s). 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.
For example:
share INTEGER iTestNum = 0
public share STRING asWeekDay [7]
share ANYTYPE aWhoKnows
void IncrementTestNum ()
access iTestNum
iTestNum = iTestNum + 1
Synchronizing
threads with
semaphores
Use semaphores to mutually exclude competing threads or control access to a
resource. A semaphore is a built-in 4Test data type that can only be assigned
a value once. The value must be an integer greater than zero. Once it is set,
your code can get the semaphore's value, but cannot set it.