Before we dig further into threads,
it's a good idea to get some sense of processors because
eventually threads are abstractions over processors,
so perhaps it's a good idea to understand some of the hardware aspects
of processors before digging deeper into threads.
So, a thread can be thought of as an abstraction over a processor.
If I create more threads,
I would like to use more processors, if available. In a typical
Windows system or any computing system really,
there are many threads, but a lot less processors, and we've seen
that. I have eight processors on my Windows 11 VM, but I have
thousands of threads that are part of the system. And obviously,
if many of those want to run, not all of them can run at the same time.
So what's really in a processor? What are some of the perhaps
interesting or maybe surprising or non‑intuitive aspects of
processors? So here's an example.
I have two pieces of code here, which are almost identical.
The purpose is to sum up the numbers in a matrix that has a
bunch of rows and a bunch of columns.
So there's a Matrix class here that's being used behind the scenes,
which is a very simple implementation just managing one big array of numbers
that are comprised of these rows and columns of the matrix.
The purpose of these pieces of code is to sum up the elements of the
matrix, and they are almost identical. On the left, we're summing the
elements by having an outer loop walking over the rows of the matrix,
and for each row, we're summing up all the columns that belong to that
row. On the right right side, we're doing the opposite.
The outer loop is based on columns.
And for every column, we're moving on to all the rows for that
column in order to sum up the numbers. And from a computer
science perspective and just intuitively we don't have to have
any official education there,
you might say what's the difference is exactly the same thing.
We're doing the same amount of work.
We're going over the matrix just once, just picking up
all the items and summing them up.
So if you use SumMatrix1 or SumMatrix2 as your function of choice,
it doesn't really matter.
I mean, it's the same thing.
Well, in practice, it seems to be quite different.