[Autogenerated] OK.
Our next step would be to create a bunch of threads,
to run the code we've just written so you can do something like this.
Let's create a thread, first to run the display function.
So I'm going to create a thread in a normal way.
I'm going to point out, to the display function.
It doesn't need any parameters at this point.
No need for any flags.
And no need for the return value uh for our thread,
ad So I'm going to just duplicate this.
Let's create four threads, the first one is going,
to be displayed the second one is going to do an add item,
the third one, is going to do an add item as well.
And the last one, is going to do remove item,
and of course,
we can play with that and add any number of threads doing various operations.
And obviously all these are running at the same time.
And the only thing left, to do here, is to wait for multiple objects.
To wait until all these threads, are done before.
We can exit the application that's going to be the
probably the most common sense thing.
To do Let's just go ahead.
And do that where all of them become signaled.
We know that for threads, signal means done.
So of course, that's going to be infinite here.
And the only thing left, to do is to close all these header want to be Nice.
Let's be nice.
So here we have the count of thread H thread here
which is simply four In this case, just going to co close the handles,
and We're done.
OK.
So let's try it out first.
Even before we introduce any kind of sem four here.
Let's see what that looks like when we run it.
So here goes, press controller, five and you,
can see that We have um item, and remove item running concurrently,
and because we have two threads running,
88 item, and only one running remove item,
its more likely that We have more items,
in the queue, then We have less items.
In the queue.
That's because we have more inserts into the or pushes into
the Q rather than than removal from the Q.
So you can see the Q size grows to a few 100 items.
Eventually its being drained out because we have more iterations,
in the their remove item K.
So it's likely it will be drained out.
Eventually the display thread takes more time,
to end Again, they're not really synchronized.
We'll fix that in a later demo.
But for now, let's focus on the size of the Q.
And Obviously the size of the Q of the Q here.
Is unlimited.
Really?
It, can be as big as needed based on.
The item, remove item uh way of running and We have that random slip, and So on.
So what we want to do here, is to limit the number of items.
To a certain number, let's say 50.
So for this purpose,
we're going to Introduce the Sem four Let's call that H S Sam here,
and create it in our main function.
So I'm going to Create H seven by calling the Create Sem up four function,
create Summa four requires a Few parameters,
the first one is the standard security two attributes
we don't really care about here.
Then we need to specify the initial count.
And the maximum count in this case since the queries empty,
we want to provide the same number for both,
which means that the count of the 74 is Now 50 it,
is signaled, and It can't go over above 50.
You can have a name which we don't need because we're not going to try to share.
That with another process.
And we know that the name is just one of the options for such sharing.
So we have the sums course its be nice.
To close the handle, to the SAML four Once we're done working with it,
so that's fine.
But how do we actually use that in code So let's start with our ed,
item function.
The add item function would like to add an item,
the Q when the Q, is not yet full.
And the Q, is Not yet full when the Summa four count,
is greater than zero, meaning it is signaled.
So we're going to call weight for a single object here on the
Summa four and Wait until it becomes signal.
Signal means greater than zero.
Initially, it's going to be signaled because it starts with the value of 50.
So that's fine.
And this is what we need.
To do here before we push an item into the where do we increment back the
74 S count this is happening when we remove an item.
When an item, is actually removed, then you can say,
OK, now we have one less item, in the queue.
So it's OK.
To add another item.
So we're going to call the opposite function called release,
74 By providing the sema four The count would like to add.
In fact, you can add more than one if that makes sense,
but one is probably the typical case and you can get also the previous
count of the summ four If you care about that In this case,
we don't just specify null here.
And this is essentially it and You have to of course, match this release.
And and Wait for single logic on the center four
Otherwise you'll get into trouble.
Notice, that This is happening by different threads.
This is what we expect from the serre call weight, but another thread.
Call real center up four This is very different than a Tex
where the same thread has to co wait, and release the TEX because the T purpose,
is very different here.
And the tes is still needed.
We can't really do without it in order to maintain the integrity of the cue.
Let's try it now.
So here goes, you can see the number of items.
In the queue can't grow beyond 50 as 50 as we try.
That's because the eight item threads when they try to add an an item,
their Wait until this summa four is signaled.
When it drops to zero, it becomes non signaled.
And at that point.
You can't add more items, you.
Are stuck in that weight state.
So eventually we'll end our application based on the number of counts.
That we've been using there.
But regardless, we can't get more items, in the queue than 50.
No matter what, it doesn't matter how fast the ed item threads work or whatever,
we always get.
That limit.
And this is exactly the purpose of the 7-Up four and Of course,
it, is fully thread safe, in the sense,
that we can work with it with multiple threads without any issues.
And that's obviously guaranteed by the kernel Otherwise,
what would be the point of having this object?
In the first place we could have used some kind of counter
but a simple counter wouldn't do the trick because you
have to make all these tests, atomic which is something,
that is not impossible but definitely not easy.
To do and would like the co to provide these support out of the box,
which it does using SAML four 4 in case.