...
In this simple example, we create an asynchronous task using an async block, which contains the code to display "HelloWorld". A finish block wraps the async which allows to wait for its complete execution before proceeding to the return statement.
Scope of variables
An async only knows about global variables and variables passed as a parameter to it (similarly to function calls). We use a set of three clauses: IN, OUT and INOUT to specify how async interacts with variables declared in the spawn environment.
- IN allows to copy a value from the spawn scope to the async body.
- OUT allows to copy a value back from the async body to the spawn scope.
- INOUT, both copy IN and copy OUT.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <stdio.h>
int main (int argc, char ** argv) {
int i = 0;
finish {
async IN(i) { // 'i' passed by copy
printf("Hello %d times !\n", i);
i--; // decr copy of 'i', only visible in the async body
}
i++; // incr original 'i' in the spawn scope
}
return 0;
}
|