[No Audio]
In this tutorial we will be looking at three
concepts related to message passing. So let's
start. We will first learn how to send out
multiple messages, I will start by bringing
the relevant modules into scope.
[No Audio]
Let me first create a channel.
[No Audio]
Next I will create a thread and we'll assign
it to a variable.
[No Audio]
Inside the thread, I will create a vector and
then I will send the values in the vector one
by one to the receiver. So let me create a vector first.
[No Audio]
Next I will iterate through the values one by one.
[No Audio]
During each iteration, I will send out
one of the values.
[No Audio]
Now inside the main thread, I can use the
variable rx to receive the values. I will use
a for loop for this purpose on the variable of rx.
[No Audio]
This syntax means that I will iterate as many
times as there are available values in the rx.
Next inside the loop I will print out the
received values.
[No Audio]
You may note that in the main thread, we are
not calling the receive function explicitly
anymore. Instead we are treating rx as an
iterator for each value received we are
printing it, when the channel is closed
iteration will end let us cargo run this now.
[No Audio]
We may also use the iter function instead of
the for loop, let us use it. I will first
call the iter function.
[No Audio]
Next I will collect
all the values in the in a suitable container
which will be i32 vector in this case.
[No Audio]
Finally, I will print all the values.
[No Audio]
Let us cargo run this now.
[No Audio]
Let us now learn how we can define multiple
producers or senders of the data, that works
with a single consumer or receiver of the
data. I will comment out all the code and
we'll start fresh again.
[No Audio]
First I will bring the relevant modules into scope.
[No Audio]
The time duration module will be used to halt
the execution of a thread for some time, I
will first define a channel.
[No Audio]
I will assume that there are two centers of
the data in this case next, I will spawn a thread.
[No Audio]
Inside the thread I will create a vector
first containing some values.
[No Audio]
Next, I will send out all the values in a
vector one by one so I will include a for loop.
[No Audio]
During each iteration, I will send out
one of the values.
[No Audio]
After sending out the value I want to give
more chance to another sending threads, so
therefore I will halt the execution of this
thread for a little time by calling the sleep function.
[No Audio]
This thread code is now complete all this
thread is doing is that it will send out the
values inside the vector one by one and after
sending each value it will halt the execution
of itself for some time before sending out
the next value. The second thread will have
very similar code to the first trend.
So I will copy the code of the trend and we'll paste it.
[No Audio]
I will just change the values inside the vector.
[No Audio]
You may note an error in this case. This is
because the tx variable has been moved, inside
the first thread and is therefore no more available.
To get around this, we will make a
clone copy of tx and we'll use this handle to
send out messages in the second spawn thread.
[No Audio]
This will enable the second producer of the
data to send out data. In general to send out
the data using the same channel we will need
to make a clone copy of the sender variable
in the main now I will add code for
receiving the values, I will iterate through
all the values that are being received using
the variable of rx.
[No Audio]
Next, I will print out the received values.
[No Audio]
Let us cargo run this now.
[No Audio]
You may note that, after each received value
there is some pause or wait and this is,
because the main thread has to wait for the
next value to be received. This is due to the
sleep function which causes the thread to
pause for some time before sending out an x
value. Moreover, the values are received from
the two threads in an arbitrary order.
The final point to note is that the values that
are sent out from a specific thread are
always received in the correct order. This
means that the values from the first thread
which are the values from 1 to 5 are
always going to be received in the same order.
That is the value one is always going
to be the first value received from the first
thread, and therefore is going to be displayed
first followed by the value of 2 and so on.
The value 2 can never be received before
the value 1. This is due to the fee for
nature of the queue. That is first in first
out, since the value one is the first one
which has been sent out by the thread 1
therefore it is always going to be received
first. The same is also true for the second
thread, the value 6 will always be the
first value received from the second thread
followed by the value 7 and so on. In
summary within the thread the execution is
always sequential and therefore the output is
always deterministic however, between the
threads the execution is not sequential and
therefore the output may be and
deterministic. Let us now move to the final
topic. I will comment out the code and we'll
start fresh again.
[No Audio]
Let us first bring the relevant modules in the scope.
[No Audio]
The final topic is related to the use of
threads inside functions, let us define a
function that will accept an i32 value
and a variable of type center.
[No Audio]
The sender will send out i32 values
therefore, we mentioned that inside the
function we will first create a thread.
[No Audio]
Inside the thread, we will print out the variable d.
[No Audio]
Next, we will send out the
variable d to the calling function.
[No Audio]
In the main function now, we will first
declare a channel.
[No Audio]
Next we would like to call
the function 5 times. So I will create a
for loop for this purpose.
[No Audio]
During each iteration, I will call the function with the
variable i and a clone copy of the sender.
[No Audio]
Next I will create a for loop for receiving
all the values.
[No Audio]
Inside the for loop I will add a print
statement for displaying the received values.
[No Audio]
Let us cargo run this and you may note
something interesting.
[No Audio]
The execution is not stopping which means
that the code is still in process. Let me
forcefully stop it by pressing Ctrl + C.
Let us explain why this happened. During each
execution of the function we are passing a
clone of the sender to the function the
function is using the actual values and
assume the ownership of the clone of the
sender. When the function is over the sender
is dropped and therefore the clone of sender
is no more in scope. However, the original
sender is still alive in the main thread as
we always send out the clone of the sender
and not the original sender. The important
point to note is that if the sender is alive
and in scope, then the receiver will keep on
waiting since it is believing that one of the
sender is still alive and he may send out a
message. As a result the program goes to a
waiting state. In summary if any of the
sender is alive for a specific channel, then
the receiver will keep on waiting for
the values in the same channel, in the hope
that someone may send out a message.
To make sure that this program gets executed
correctly, we will make sure that the sender
tx is dropped before we start to receive the
values using the for loop. This code will now
go to completion since all the senators are
done with their job. The last point to note
is that it is not necessity that the value 0
will always be sent out first followed
by the value 1 and so on. Let me execute a
few more times and you may notice.
[No Audio]
The code inside the thread although runs in
sequential order, however, during each call
inside the function we are creating the
threads so therefore the values may be sent
out in different order. Finally, the value
which is sent out first is always going to be
the values which is received first. This is
again related to the fact that the channels
implemented for order that is first in first
out that is it for this particular tutorial.
I hope you would have enjoyed this see you
again and ultimately next tutorial.
Happy Rust programming.
[No Audio]