[No Audio]
Welcome back again. This tutorial is about a
special feature of Rust for threads called Park.
The park will block the execution of
the current thread until it is unparked. You
may find it quite useful in your
synchronization problem. So let us start
learning it. I will first bring the relevant
modules into scope.
[No Audio]
Next, I will create a thread called job_1.
[No Audio]
Inside the thread, I will add a
print statement to indicate that the
thread has started.
[No Audio]
Next, I want to stop the
execution of this thread for some time, so
that another thread containing job_2
is started and goes to completion. So I
will add a print statement to indicate this.
[No Audio]
To stop the execution of the thread or
suspend its execution for some time, we need
to use the park function.
[No Audio]
This will block the current thread or job_1,
until we explicitly resume it
using the unpark function. When the thread is
unparked, the remaining of the code in this
thread will be executed. I will add a suitable
print statement to indicate the resumption of
the thread code.
[No Audio]
Let us now define another thread called job_2.
[No Audio]
We want this thread to
complete before the thread with handle of job_1.
I will just include some
simple print statements inside this thread.
[No Audio]
Outside the thread, I will call the join on
job_2 to make sure that it
goes to completion first.
[No Audio]
I will add a suitable print statement to
indicate that the job_2 thread is now complete.
[No Audio]
Next, I want to resume the job_1 thread.
First, I will include a suitable print statement.
[No Audio]
Next, I will make a call to the
unpark on job_1, this will
resume the execution of the job_1 thread.
Finally, I will make sure that the
job_1 thread goes to completion,
so I will call the join on job_1 thread.
[No Audio]
Let us cargo run this now a few times.
[No Audio]
You may note that during each execution, the
job_2 is first completed, and
only then job_1 goes to completion. Similar
to the thread park, there is another function
called park_timeout. This will
essentially block the current thread for some
specified time interval. The thread will be
unblocked if during this interval the thread
is being unparked. If no unpark is being
called, then the thread will be blocked
until the time duration ends. Let us see this
in the same program. Instead of the thread
park function, I will now call the thread, the
thread::park_timeout function with
some duration in seconds. This will
essentially blocks unless or until the
current thread token is made available, or the
specified duration has been reached.
Now what will happen is that, the job_1
will be blocked until the duration of 2
seconds expires, or when it is being unparked.
Let us execute.
[No Audio]
In this case, the thread or job_1
resumed immediately before the expiration
of the time interval. Let us comment out the
thread unpark for job_1 and execute again.
[No Audio]
In this case, you may note that it took some
time. This is because the thread
corresponding to job_1 was
parked for two seconds and it completed the
two seconds since nobody else unparked the
thread. Please note that there is a very
similar function to the thread park, and the
thread::park_timeout called a thread::sleep.
Let me also use it.
[No Audio]
This function will block the
current thread for the specified amount of
time. The main difference of this function
with the thread::park_timeout function is that,
this function will completely block the
thread for the indicated amount of time with
no way of resuming back thread before the
time expires. Even the calls to the thread
unpark will not work. Let me comment out the
thread unpark function and execute now to confirm.
[No Audio]
Lastly, there is another useful
function called yield_now, when a thread
calls this function, it will cooperatively
gives up its turn to the operating system,
telling the operating system that I am
willing to give out my running time to
someone else. This generates a signal from
the calling thread that is, it is willing to
give up its remaining time slice so that the
operating system may schedule other threads
on the CPU. Let us use this function in the
job_1 thread, I will comment out
the call to the sleep function.
[No Audio]
Now, let us call the yield_now function.
What this will mean that the job_1 will
yield back to the operating system, which will
enable the operating system to schedule some
other thread for execution. The operating
system will therefore either
execute the job_2 thread or the
main thread. The overall effect of this will
be that the job_1 may go to completion late,
because it is yielding back and therefore
giving more chance to the job_2
and the main function. Let us execute a few
times to see the result.
[No Audio]
You may not get the job_1 is always
completing late due to the fact that it
is yielding back to the operating system.
That brings us to the end of this tutorial.
See you again and until next tutorial.
Happy Rust programming.
[No Audio]