capri, a script language

Home | Documentation | Releases | Sources | Contact

Concurrency

To run a statement concurrently, you can use the concurrent expression. Executing such an expression results in a value of type concurrent_id. To retrieve the return value of a concurrently executed statement, use the join expression passing the concurrent identifier that was created.

This example shows how to use these expressions:

import io;

task all {
	fib_concid = concurrent fibonacci(25);
	
	IO.println("calculating fibonacci(23)...");
	fib_23 = fibonacci(23);
	
	IO.println("waiting for fibonacci(25)...");
	fib_result = join fib_concid;
	IO.println("fibonacci(23) = {fib_23}");
	IO.println("fibonacci(25) = {fib_result}");
}

function fibonacci(i) {
	if(i <= 0)
		return 0;
	else if(i == 1)
		return 1;
	else
		return fibonacci(i - 2) + fibonacci(i - 1);
}