[Autogenerated] Back.
To our increment example, we know that We get a be count the lapse time.
Is three or four milliseconds.
Let's see what happens if we try to fix the data
raise which is something we really.
Want to do in the general case.
So instead of using count, plus plus I can Go ahead.
And change, that to interlock, increment,
and provide the address of this count variable.
In fact, this function requires an unsigned value.
So that's going to be fine.
I'm just going to cast, that to an unsigned integer,
uh which is the same thing unless we overflow.
Let's see.
What happens now when we run this as you can see it
runs now perfectly the count.
Is exactly what we expect of 10 million here.
Their lapse time.
However, is much bigger.
It seems.
To be 117 milliseconds or something.
And You might be thinking, wow, that's a big performance difference.
It's not really that bad.
Remember this application, is is very artificial.
We have 10 threads, that are _______ on the same variable incrementing,
eight million times this is not a very useful application.
To have and So in practice these instructions,
interlock instructions, are actually very fast.
So, you shouldn't take this number as to suggest that using them is a bad idea.
In any case,
you can't get better than that In this particular artificial example,
obviously, we need to use other examples.
To see what's going on.
But this is just a way, to prove, that at least.
We can fix, the database Now, by the way,
is a Side note, if using C++ C++ 11, and later,
to be more specific, you can actually fix that in a different way.
And So it is possible.
To use the STD atomic variable for example.
So if I go ahead and add atomic here, and then I'm going to Use the count here.
To define that as an atomic object.
Let me just do that as a global variable.
Just to make it simpler.
So, I can do atomic of integer here, let's call that count,
and initialize, that to zero.
So In this case, if I go ahead,
and uncommon this and simply write here something like count.
Plus plus like normal.
In this case, this count doesn't mean anything anymore.
I don't need that and I don't need to pass.
Anything here.
I could of course have done the same thing for the count, original variable.
Put in the global variable, but that's not the point the point here.
I can use count,
plus plus and still get the correct answer because behind the scenes,
we have uh the appropriate interact function being
actually used there behind the scenes.
In fact, so.
Let me just go ahead.
And and try to Uh fetch the the value here.
Is to load here.
So the compiler is happy and we can run,
that and we still get the correct answer.
Notice the number,
is roughly the same and That's because STD atomic does
interact increment behind the scenes.
That makes sense because that's the best you can.
Do and So in fact, if you are using C++ 11,
and later,
these kinds of operations like plus plus plus equals and all that is
implemented by the atomic class in the correct way.
That uses the compiler intrinsics,
to have the best performance possible but still maintain data raise free code