Previous: Examples Up: Examples Next: Finding a Minimum Element
This program displays the traditional greeting "hello, world".
#include <iostream.h>int main() { char *s1, *s2; par { s1 = "hello, "; s2 = "world\n"; } cout << s1 << s2 << endl; return(0); }
The assignment of "hello, " to s1 and of "world" to s2 can occur concurrently or in some arbitrarily interleaved manner. Perhaps the operations required for assignment to s1 are executed, and then the operations required for the assignment to s2 are executed. (Such a sequence of operation is identical to the execution of the sequential program created by removing the keyword par.) Perhaps s2 is assigned to the string "world" first, and then the assignment of s1 occurs. Perhaps the operations for these two assignments are interleaved in some manner or perhaps they occur in parallel. Regardless, because s1 and s2 are distinct mutable variables, these operations are guaranteed to be noninterfering. Hence, at termination of the parallel block, we know that s1 is the string "hello, " and s2 is the string "world".
The program therefore results in the message ``hello, world'' being displayed every time, regardless of the actual order of operations.