[Autogenerated] In our example.
So far we've used some numbers here.
Just to have a number of iterations running and doing these
various things like adding removing items.
One of the things we have here.
Is the display function, display function,
that is called by a single thread, is running 2 2000.
And displaying items as the number of items changes with a small slip here.
Now it would be perhaps more efficient to have the display thread,
thead that's running the display function.
To end its life when the remove item, and end item threads,
are done because at that point the querying to be making any changes.
So why keep this thread alive.
And So perhaps we can do something with events when
the three threads here add item, add item,
remove item, are done.
We would like to signal something.
That would cause the display threat, to to bail out,
to simply exit as soon as possible because once these three threads,
are done, there's really no point in in continually running.
So one way, to achieve that is By using event objects.
What we'll do we create an array of event objects or three event In fact,
we'll give each one of these threads.
One of these events, add item, add item,
and remove item, these are three separate threads or the two of them.
Are running the same function.
They're still separate threads.
And When each one of them, is done, they're going to signal,
their event indicating that they're done.
The display thread will look at these three events,
and Once they're all signaled, is going to bail out.
That's going to be our idea here.
So first let's create these events.
I'm going to Use the create event a P I,
to create event API I needs the following parameters.
First one is the security two attributes as usual which
are not interested in in this course.
Then we need to specify what kind of event we would like.
That event to be either manual reset by specifying truth or
auto reset by specifying false In this case,
it doesn't really matter.
That much because they are going to be just one word data,
that would be the display thread on that event or these
event So it doesn't really matter, but we'll have to select something.
Let's go with the manual reset.
The initial state of the event should be false should be
down because we want the display thread,
to be running initially,
and not bail out because the other threads will still be running.
And then we can provide an optional name.
In this case,
we don't need it because we are not going to use those
events outside the process here.
So the threads don't need to have Any particular way of sharing,
you're just going to use these variables.
Uh as we pass them, to these threats.
So we have just these three events, and what I'm going to do here,
I'm going to pass, the eight item function,
the first event and another thread here running an event I'm going to pass,
the second event and Here.
We're going to pass, the third event and for the display thread.
We're going to pass the entire array of events that's going to be the pointer,
to that array.
So now that we have these events in place.
We need to Actually use them in some constructive way.
Of course, we need to add some code here.
To close the handles eventually.
To be nice.
But I'm going to skip, that to save some time.
So here's what happens when the let's start with remove item,
and add item when these are done.
They need to set, their event So now the pointer here,
is not going to be Just now it's going to be the event itself.
And since an event is a handle, which is the same as a pointer from a C,
C++ typing perspective, we can just go ahead.
And use set, event simply cast, that value, to an event object.
That's going to be our code here, to say that we're done.
Same thing goes here, we're done.
And In this case, we know that we're going to call two threads,
or create two threads, running, eight item,
but it doesn't matter which.
One would get its own handle, its own event So the fact,
that we have two threads running the same function doesn't really matter.
And of course,
we can combine that in more interesting ways by adding more functions here.
More threads, sorry,
that would use these existing functions this is all something.
We can do We just need to create the number of events as the same
number of threats except for the display threat.
The last piece of the puzzle.
Is to go, to the display function.
And not have a loop here, but instead have a wire here.
That's going to call the weight for multiple objects function.
Here, we're going to get some point.
And That's going to be appointed to an array of handles.
Let's just go ahead.
And perform the casting here simply by casting the pointer P here.
To an array of handles, a pointer,
to a handle really we know that We have three handles.
We have to kind of know that In some way,
I didn't pass that although we could have done that
by passing some custom structure, but let's just keep it simple here.
And wait for all.
These events, these three events, wait until all of them.
Are signaled, and wait zero time.
So I don't really want to block at all.
I'm just saying if looking,
examining these state of these three events result in them being signaled.
Then we want to to bail out of these loop So if we get
here something like wait time out.
It means these are not signal,
we can continue working because all these these three threads,
these other threats.
Are still working, at least one of them.
Is still working.
So this is one interesting case we use the time out of zero.
And not infinite.
We don't want to block here we just want to examine,
to kind of pull the state of these three events,
and that is it.
That's all we need.
To do here.
Let's try it out.
So what we expect.
To find, is that the display thread ends immediately after the last thread.
Usually that would be the remove item thread because it has more iterations,
to work with.
So from a at least for the probabilistic perspective,
its going to end the last and The display should end
immediately after that without having to wait for some
kind of counter to uh to be done.
When eight item threads, are done the remove item thread, is still working.
And you, can see that the display, and remove item, are done.
At the same time, the fact,
that we had the display thread here showing its display,
first the text that's just a matter of er,
raise condition between the print,
ef that This one is doing and the print ef that This one is Doing so it
could be in the opposite in the reverse order here.
But that of course, doesn't really matter.
So we achieved what we wanted.
To achieve.
In fact, we can add another event here.
Perhaps by saying if all the eight item threads,
are done, the remove item can go ahead.
And exit.
If the number of items, in the queue reaches zero because at that point,
there's no way.
That more items will be added into the queue so you can use another event here.
In order for the remove item, to Look at the other existing ad item threads,
and bail out when there is No work.
To be done when the queue, is empty, and There's no way.
More items will be added because the item threads, are done.
I leave that as an exercise for you.
Dear viewer.