User`s manual

Dynamic C Users Manual digi.com 65
The indexed cofunction call is a cross between an array access and a normal function call, where the array
access selects the specific instance to be run.
Typically this type of cofunction is used in a situation where N identical units need to be controlled by the
same algorithm. For example, a program to control the door latches in a building could use indexed
cofunctions. The same cofunction code would read the key pad at each door, compare the passcode to the
approved list, and operate the door latch. If there are 25 doors in the building, then the indexed cofunction
would use an index ranging from 0 to 24 to keep track of which door is currently being tested. An indexed
cofunction has an index similar to an array index.
waitfordone{ ICofunc[n](...); ICofunc2[m](...); }
The value between the square brackets must be positive and less than the maximum number of instances
for that cofunction. There is no runtime checking on the instance selected, so, like arrays, the programmer
is responsible for keeping this value in the proper range.
5.5.5.2.1 Indexed Cofunction Restrictions
Costatements are not supported inside indexed cofunctions. Single user cofunctions can not be indexed.
5.5.5.3 Single User Cofunction
Since cofunctions are executing in parallel, the same cofunction normally cannot be called at the same
time from two places in the same big loop. For example, the following statement containing two simple
cofunctions will generally cause a fatal error.
waitfordone{ cofunc_nameA(); cofunc_nameA();}
This is because the same cofunction is being called from the second location after it has already started,
but not completed, execution for the call from the first location. The cofunction is a state machine and it
has an internal statement pointer that cannot point to two statements at the same time.
Single-user cofunctions can be used instead. They can be called simultaneously because the second and
additional callers are made to wait until the first call completes. The following statement, which contains
two calls to single-user cofunction, is okay.
waitfordone( scofunc_nameA(); scofunc_nameA();}
loopinit()
This function should be called in the beginning of a program that uses single-user cofunctions. It initializes
internal data structures that are used by loophead().
loophead()
This function should be called within the “big loop” in your program. It is necessary for proper single-user
cofunction abandonment handling.