[Autogenerated] Let's now see an example of a termination
handler using the tri finally construct.
So I have a small console application here which just includes a few headers,
one of them, is the vector,
header which is the one containing the implementation
of the C++ standard library vector, plus So let's say we have a vector here.
Let's say a very simple vector, wi as a global variable.
Let's call that data and the problem, is that Well,
let's first write the function called add and The
purpose of add is to add an item,
to the back or to the end of this vector which is a resi able array.
So the way to do that is to use the pushback function
like soap sounds simple enough.
We can Go ahead.
And call these ed function as as many times as we like.
So what is The problem here, the problem,
is that If the ed function, is being called by multiple threads at the same time.
Then we have a database on our hands,
which means that This vector could be corrupted.
We'll discuss synchronization of threads,
and why we want to do that and how to do that in more detail.
Later on in this course.
But for now I want to show you just a brief example of how to handle.
That the basic idea, is trying to acquire a synchronization object.
So, that only one, third at a time can acquire it.
Let's use something here called critical section.
So let's just create one of those again as a global variable for simplicity,
and the way to work with the critical section.
Is by calling enter critical section here,
and providing the address of the critical section.
This would acquire the critical section by the current thread.
If the critical section, is currently being held by a different thread,
then we'll add a will enter weight um here.
So the thread will enter weight state without consuming any CP cycles.
Once its able to acquire the critical section, it can perform the operation.
And then call the opposite function.
Left critical.
Section, in order for the critical section,
to be released so other threads can acquire it.
So assume just for demonstration or for conceptual purposes.
That you have multiple threats here calling these ad function,
possibly at the same time.
Want to make sure the vector is not being garbled,
and That's why using these critical section,
is very important.
However, this code is not bulletproof,
and That's because the pushback function might raise an exception.
If for example, the vector has to be res sized,
and enlarged and maybe we're out of memory for some reason.
And of course, in general, the code here might be more much more complex,
and could even call other functions which we don't
necessarily have control over.
So the problem.
Is if there is an exception happening here.
A call stack is going, to be searched the one that's starting from this location.
And the problem.
Is that leave, your critical section will not be called,
which means the critical section will continue.
To be held even if the exception.
Is handled in some way, other threats will never be.
Able to get the critical section again.
And will have a deadlock on our hands.
And this is where try finally can help out.
So, I can add, a try finally block here.
And I'm going to Say whenever we get out of that tribe,
blog I'm going to call these critical section,
no matter what.
Even if there is an exception somewhere.
In the tri block, and of course, again, could be much more complex,
code here calling other functions.
That might call other functions.
That are not necessarily up by my control, in the that respect.
I have no idea what these functions.
Are necessarily doing I want to make sure that I don't get a deadlock on my
hands by calling live critical section no matter what.
So there's no way, to circumvent, that Finally,
even if we have some test here, for example,
like if the vector size, let's say is too big.
And I would like to In this case, return, and not add the value still.
That return will force calling leave critical section again.
So there's no way.
To go around, leave critical section which is exactly what I want.
And So it's sometimes easier.
To work with finally because you are guaranteed,
a piece of code will run and Even if you get out of
the blog in a nonstandard way,
like using return or go to whatever you still can't circumvent finally.
So this is a typical case where try finally makes sense.