What are threads? Consider the following code segment that computes the product of two dense matrices of size
.
1 for (row = 0; row < n; row++)
2 for (column = 0; column < n; column++)
3 c[row][column] =
4 dot_product(get_row(a, row),
5 get_col(b, col));
- The for loop in this code fragment has
iterations, each of which can be executed independently.
- Such an independent sequence of instructions is referred to as a thread.
- In the example presented above, there are
threads, one for each iteration of the for-loop.
- Since each of these threads can be executed independently of the others, they can be scheduled concurrently on multiple processors.
We can transform the above code segment as follows:
1 for (row = 0; row < n; row++)
2 for (column = 0; column < n; column++)
3 c[row][column] =
4 create_thread(dot_product(get_row(a, row),
5 get_col(b, col)));
- Here, we use a function, create_thread, to provide a mechanism for specifying a C function as a thread.
- The underlying system can then schedule these threads on multiple processors.