Chapter Contents

Previous

Next
Coprocessing Functions

cocall and coreturn

The cocall and coreturn functions are used to transfer control and information from one coprocess to another. Normally, the cocall function is used to request a service of a coprocess, and the coreturn function is used to return control (and information about the requested service) to a requestor. The transfer of control from one coprocess to another as the result of a call to cocall or coreturn is called a coprocess switch.

Use of cocall is very similar to the use of a normal SAS/C function call. They both pass control to another body of code, they both allow data to be passed to the called code, and they both allow the called code to return information. Whether you are using a function call or a cocall , after completion of the call the next SAS/C statement is executed.

In contrast, the SAS/C return statement and the coreturn function behave differently, even though both allow information to be returned to another body of code. Statements following a return statement are not executed because execution of the returning function is terminated. However, when coreturn is called, execution of the current coprocess is suspended rather than terminated. When another coprocess issues a cocall to the suspended coprocess, the suspended coprocess is resumed, and the statement following the call to coreturn begins execution.

The following example illustrates the way in which cocall and coreturn work. The two columns in the example show the statements to be executed by two coprocesses, labeled A and B. For simplicity, in this example no data are transferred between the two coprocesses.

     Coprocess A                        Coprocess B

A_func()                           B_func()
{                                 {
   .                                  .
   . /* other statements */           . /* other statements */
   .                                  .
begin:                                coreturn(NULL);
   puts("A1");                        puts("B1");
   cocall(B, NULL);                   bsub();
   puts("A2");                        puts("B4");
   cocall(B, NULL);                   coreturn(NULL);
   puts("A3");                        .
   .                                  . /* other statements */
   . /* other statements */           .
   .                                 void bsub()
}                                    {
                                           puts("B2");
                                           coreturn(NULL);
                                           puts("B3");
                                           return;
                                      }

If coprocess A's execution has reached the label begin , and coprocess B is suspended at the first coreturn call, then the following sequence of lines is written to stdout :

   A1
   B1
   B2
   A2
   B3
   B4
   A3


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.