[Autogenerated] the most important reason for creating threats in the
first place is to be able to run code concurrently to utilize the
multiple processors we have on systems today.
So I'd like to show a more complex example of utilizing more
processors and more threats of course in order to perform a
calculation faster instead of using just a single threat.
And what we want to do here is to create something which
is a multi threaded primes counter.
So let's say we have a bunch of numbers from some starting number two,
some ending number.
And you want to count how many prime numbers are in this range.
So a simple way of doing that is to just run a single loop that
transformed the from number two the two number and then check each
number to see whether it's a prime number and if so increment a counter
and that would definitely work but it will be using a single process or
attending given point in time, which is not as efficient as it can be.
Maybe we can use multiple threads and multiple
processors to make it happen faster.
So we'll use the following simple idea.
Let's chunk the range into these pieces.
Each one would be processed by a single thread.
So for example, if you want to use five threads,
we'll just chop up the range into five pieces and give
each thread its own range to work with.
And of course there might be some leftover tail at the end because
the the division might not be exactly right,
right.
And we have to give that uh that tale of numbers to some threat to work on.
And then we can get or however those results and combine the counts.
Each one of these threats has accumulated to get the final result.
And we'll do that in a way that would allow us to customize number of threads
we use in order to see the changes we get as as we use more threats in
relation to how long it takes to make this calculation.