[Autogenerated] Let's build something.
That demonstrates the usage of Sema four So we'll do that In two steps,
the first one would be to use a qna Q,
is Just uh what you might imagine as a Q being a first
in first out kind of data structure.
So I'm going to Use the Q class from the C++ standard library,
and just going to create a global variable of the what we want to do here,
is to make sure that We have multiple threads,
adding or removing items from that queue,
that the number of items, in the queue doesn't grow beyond a particular limit.
So first,
we need to Make sure the Q doesn't get data raise by using multiple threads,
the Q, plus is not a thread safe.
So we're going to use the TEX here.
In fact,
that's the same kind of technique we've used in the
previous module with the TEX.
So let's go ahead.
And create that Tex, first in our main function.
Let's create the TEX by calling create mu attack We're going to Start the
TEX in the non in the signal state really its free.
So any thread can grab it.
And the only difference between these and the mutes.
We've used in the Previous demo, is that I'm not going to Give that Muti,
a name, and the reason, is that We're not going to share,
these data across processes but these data here,
the QE is shared between threads,
that are going to be running within the same process.
So there's no need.
To to share the Tex in any particular way.
All the threads have access to this handle without any issues
because they're running as part of the same process.
And Now let's create a few threads,
to do some work over that So I'm going to create the threads very soon.
But let's create for now the functions themselves,
the worker functions for the threads.
So one of them, is going to be add item, the purpose of this function.
Is to add an item, to the Qing item into the queue.
So first let's write something.
That says that the add item thread start it.
I just want to have something.
To correlate, and See what's going on when we run this thing later on.
And then Let's do the following.
Let's take a loop running from zero.
Let's say, to 500, and the purpose of this loop is to add an item,
uh to the querying Waiting until it becomes available.
Then We grab it.
And continue execution.
And simply push some value into the queue.
It doesn't matter which value we push.
That's not the point.
I'm just going to push something.
So we have another item, in the queue.
If I go ahead.
And release, Tex here, we can Go ahead.
And do that 500 times, and maybe just to make things.
A bit, more random.
Let's just add some sleep factor here.
So I'm going to Use, get the count as a way.
To provide the number of milliseconds, and labs.
Since Windows started.
And add some small value.
So we have some sleep here.
Just to make things a bit,
more interesting and Essentially we're done the only thing left.
To do here, I'm going to Just add an other print, app here.
To say that eight item, thread, is done,
and Just display the thread, ad Let me add the back.
Slash end here, and I'll Show you why I want to do that a bit later.
So that's going to be the ed item function,
its purpose, is to add items, to the we can have something similar.
Let's have another thread.
Well, it's.
Still not a thread, just a function, but it's going to be run by a thread.
At least one, let's call that remove item, and its purpose.
Is to do the opposite to remove items from the.
So here, is remove item.
So what we need, to do here, is first,
to acquire the T text just like before because we
want to make access to that shared object,
the Qan then If the Q is not empty then we can pop,
an item out of there.
So here goes, per we're ping an item.
We're not doing anything with it, but in a real scenario,
that we move item, we'll probably take that item,
and do something with it.
To process, that In some way, then We're going to release the mute text again.
And again, have that little slip here.
To make things more random, a third thread,
I would like to add,
is going to do something simpler which is just display the number of items.
In the cue at any given point in time when that thread, is accessing the queue.
Let's call that thread or that function at this point display.
So we have display here.
Let's just uh change, that to display, and what I want to do here,
is to run this uh say 2 2000, maybe.
Their remove item should be running more Let's do that uh like 15 1500.
We'll see.
We'll tweak, that later on and Discuss what these numbers mean if,
if any and uh and what we want to do here,
is simply to display the number of items.
In the in the cube.
Let's just display the number of items by doing something like this.
I'm going to Use the space here.
To display the number of items.
In the cue by calling the size function.
Now, technically it's um a good idea, to call rate for single object,
and release, xss but I'm not going to do that here.
In fact, I'm introducing here, very small potential.
Data raise and and That's uh in this case,
that's probably OK because the size itself,
is just a number and its being actually accessed in a fairly atomic
way because of the behavior of the processor.
But it is a database nonetheless, in the sense,
that We're reading from some data probably the count
which is the size of the data structure.
While at the same time another thread might be popping or pushing items into it,
changing that size.
So I'm t cheating here.
A little bit, just to make things a bit easier.
To see want when we run it.
But you're welcome.
To Try, to Use the Tex as well here while protecting the queue.
And We'll talk about some perhaps better options.
A bit later.
Ok.
So we have something like this just displaying every so often the size.
But in fact, let's do something slightly better.
Let's look at the size when we get started.
And let's only display the size if it's different than.
The the current size of, of the cube.
So if it's different then we'll display that and update our size,
by the way, these non equals sign, of course,
is just a normal, not an equals.
I'm just using the Cascadia code font which has these nice literatures,
to connect certain characters, to have a different look,
but it's still the same old see or C++ you're familiar with.
So now, that we have these three functions in play.
We can Go ahead.
And use them.
And then See how to combine, a Sema, four into the mix.
We'll do that in the Next clip