The example in Wikipedia that you're referring to is from MIT Cilk, which required that all calls to Cilk functions be spawned. That requirement was removed by Cilk++, which evolved into Intel's Cilk Plus implementation.
Keep in mind that it's the continuation that's stolen, not the spawned function. The worker that executes the code before the spawned function will execute the spawned function. The cilk_spawn
pushes the continuation onto the worker's deque, making it available for stealing by an idle worker.
Consider the full implementation of fib in Cilk Plus:
int fib(int n) {
if (n < 2)
return n;
int x = cilk_spawn fib(n-1);
int y = fib(n-2);
cilk_sync;
return x+y;
}
If you put a cilk_spawn
on the second recursive call to fib, then you're allowing an idle worker to steal the continuation after that call. But that strand immediately ends at the cilk_sync
, so it's a waste of time.