This section uses the computation of the Fibonacci numbers as an example to illustrate the direct use of the Intel Threading Building Blocks task scheduler. A Fibonacci number in the Fibonacci series *F* is defined as the sum of the previous two terms:

F_{n} = F_{n − 1} + F_{n − 2} |

Therefore, the seventh Fibonacci number of the series beginning with F_{0} = 0, F_{1} = 1 is 8 (F_{6} = 8). This example uses an inefficient method to compute Fibonacci numbers, but it demonstrates the basics of a task library using a simple recursive pattern.

To get scalable speedup out of task-based programming, you need to specify a lot of tasks. This is typically done in Threading Building Blocks with a recursive task pattern.

Example 9-1 shows a traditional, serial solution using recursion.

Example 9-1. Fibonacci serial version

long SerialFib( long n ) { if( n<2 ) return n; else return SerialFib(n-1)+SerialFib(n-2); }

The top-level code ...

Start Free Trial

No credit card required