[Autogenerated] Let's demonstrate these kind of database and then See
how we can solve it by using various mechanisms.
That are provided.
So for now, I'm going to have a Very simple example.
Here, here's the console application.
I'm going to create some variables.
To work with like loops which is going to have a million loops.
We'd like to use and then Let's use a certain number of threads.
Let's go with 10 just as an example,
but it could be really any number I just want to amplify the problem of a
database So what I'm going to do here in my main function,
is to create a bunch of threads as the same number.
That we find here.
So let's do that Before we do that here, is the counter.
We'd like to increment.
Let's start with the value of zero and then create this number of threads.
So let's go ahead.
And have a loop here.
And create the thread.
So we have H thread at index I its going to be equal,
to create thread, its going to create a simple thread here.
That's going to perform an an function called do ink
which we'll implement in a moment, and We pass along the address of dot count,
variable, to this function, and the rest of the parameters.
Are not really that interesting.
So no flags, and no need for the return for an ad and All we need to do here,
really?
In the this loop Let me just put curly braces here.
Just to be Nice.
We just need to Wait for all these things,
to end So I'm going to Use the weight from multiple objects
which we've used in previous courses,
which I'll also discuss in the This module Later on,
we have the number of threads.
To wait on that would be the count of Hre that's going
to be simply 10 we can Just go ahead.
And just write threads here.
That's good enough.
And then We have the array of threads.
And then We need to Wait until all of them.
Are done, and We're going to Wait for as long as it takes.
But before we actually do that Let's try to measure the time
it takes to make this operation works.
So I'm going to define a data structure called large integer here.
Call its start.
I'm going to call a function called query performance counter.
This is a way,
to measure time in a very precise way on Windows This will return a value,
just a 64-bit value,
that represents the performance counter which is just a number that's
incremented in very fine units with fine granularity,
high resolution as as high as the system would allow.
So, that would be start.
And then Once we're done.
I need to do the same thing again.
And calculate the difference there.
So let's call another query performance counter here,
and get back end, and we can Go ahead.
And display the results.
So the elapse time, let me just calculate the elapse time.
First is to take end and quad part,
is just the 64-bit number there because originally it
has these 32 32-bit numbers in it.
So now, we can just grab these 64-bit value, and calculate the difference.
And go ahead.
And display the results.
So what we get here, in the count, is whatever we have.
In the count, variable, and then What?
Is the expected value.
We're going to do that um incrementing million times,
per thread, and we have 10 threads.
So we expect to have here, a number, which is going,
to be 10 times, a million,
which means simply 10 million and Let's look at the el apps time here as well.
Which is going to be in micro seconds.
This is what I want, to have here.
So we have count as what we actually achieved.
And then what we are expecting to Find if everything goes as planned.
Is threads, times loops.
I haven't yet written the two ink function.
Its going to be happening very soon.
And then We just need to Look at the elapsed value here.
An elapse, is actually in units of 100 100 nanoseconds.
So in order for that to be in in microseconds, we actually have to divide by 10.
So let's just do that and That's it.
We're done almost.
We have to write the two ink function.
That does all the work.
So we have a parameter here, that We get and this is the counter,
we need to to increment.
I'm going to Go ahead.
And reference, that counter its important to reference it.
I don't want to just copy it locally because then I will just
increment the local variable which is not the point.
We.
Want to increment, that shared variable.
If you are not comfortable with C++ and with references
You might use the see alternative,
which simply means just go ahead and and use the pointer here,
just cast it.
And then just increment star count.
So you can do something like this.
And That's fine as well.
But I'm going to stick with C++ here because I think it's a bit, more intuitive.
So now we need to have a loop here running from 02 loops, and the only thing.
We do here.
Is simply count plus plus that is it,
and whatever we return doesn't really matter.
Nobody cares.
And of course, we need here.
To also close the SS but I'm not going to deal with that It's
not very important for our purposes here.
Let me build that see If everything compiles as expected,
and it does let me even make sure that I'm building here in release build,
to Make sure we get the best code The compiler can
generate and then Just go ahead.
And run it.
Press OLE five to run it.
Here's what we get.
Notice what we get the count.
We get.
Is three million something.
We expected 10 million,
but we got three million something which means we lost various increments there.
And this is due,
to data arrays because we have 10 threads accessing the same memory location.
And at least one of them.
Is changing.
In fact, all of them are trying to change,
that memory location, and It takes about four milliseconds.
To run all these pieces of code here.
Let's try it again.
Let's press control F, five again, and You notice the result,
is different and that be co That's because we're talking
about something which is probabilistic here.
Exactly when these process sources, are accessing the same variable,
sometimes the same time, sometimes not so much.
So the account here.
Is different, but obviously it's wrong.
So we have a database on our hands.
Now, of course, this example, is contrived it's unlikely.
To have real application doing something like this.
But this is just to prove, a point.
In fact, if you have these increments happening every now.
And again,
it's much more difficult to Figure out you actually have a problem because
you might run up up your application like 1000 times,
everything seems OK.
But then you get these error and then good luck.
Reproducing, that as I mentioned already.
So, in the next clips.
We'll see how to resolve, these and other kinds of databases.