51 How to Implement Thread Synchronization and Thread Communication in Python

preview_player
Показать описание
Hey guys,

welcome to Free Tutorial TV and Python tutorial for beginner .

In this video we will see how trade synchronization works in the case of multi threading in Python.

And we will talk about how to use locks in the case of trade synchronization.

So first of all what is trade synchronization so trade synchronization is defined as the mechanism which

ensures that whenever two or more trades are executed in parallel or concurrently and if they are sharing

the same resource in that case all the trade should not work simultaneously or partly on the same resource.

So let me explain the thread synchronization problem with the diagram.

So in this diagram you can see there are three threads and all these threads are sharing the same resource

it can be a global variable for example.

So as long as all these threads are accessing this shared resource one by one not at the same time everything

will work fine.

But the problem arises when two or more trades try to access the same global variable or shared resource

at a particular time.

And this kind of situation is called the race condition so a race condition occurs when two or more

threads can access the same shared data and they try to change the value of this same shared data.

At the same time and whenever this kind of race condition occurs the value of the variables may be unpredictable

because if two trends are working on the same global variable at the same time you will never know which

thread has worked on this variable.

So the result is the unpredictable value of a variable.

Now let me explain this problem with an example.

So I have this simple code in which in the first line I am importing threading module and in the second

line I am just declaring a global variable.

Now I have two functions here.

One is thread task and other is main task.

Now inside this thread task function I want to increment the value of X every time by 1.

So this is the code for this and I want to increment this value by 1 10 times.

So that's why I have used this for loop in the range 0 2 9.

So it's going to increment the value of x 10 times inside this loop.

Now because this X is a global variable I have just added this global keyword in front of x.

So our function knows that this X is not a local variable it's a global variable.

So this function is simple enough.

It just increments the value of x 10 times inside the loop and this X is this global variable which

we have declared outside this function.

Now in the main task what I'm doing is I'm creating two instances using the threading module with the

help of thread class.

This we have already seen in the last video How to Create threads using threading module.

So nothing new in this.

I have created two instances T1 and T2 and I am passing the target is equal to the function name which

is this function which is try to task so board that thread will call the same function try task and

then I'm just starting both the threads with the help of start function and then I'm just using the

joint methods on both of the instances.

Now here inside our main if condition I'm just calling our main task in which our thread are created

and executed and then at last I want to print the value of X when all the thread execution has been completed.

So let's run the script and let's see what happens.

**************************************************************************
please like and follow us in Facebook and Instagram.

Рекомендации по теме