In the previous lecture, we got done
with discussing the model of a parallel machine.
And in this lesson, what we're going to start
doing is talking about synchronization algorithms that goes
into the guts of any parallel operating
systems that is supporting multi-threaded applications. And as
we discuss the synchronization algorithms watch out for
attacker's quote that I mentioned in the previous
lesson on sharing, in shared memory multiprocessors
that is going to be very, very key
in terms of understanding the scalability of
the synchronization algorithms. Synchronization primitives are a
key for parallel programming. In your first
project, you implemented a threads library, which
provides the mutual exclusion lock. Let's talk
about locks. What exactly is a lock?
Well, you know, in the metaphor that you
know about in real life. Lock is something that
protects something that is precious. And in the
context of parallel programming, if you have multiple threads
executing and they share some data structure, it
is important that the threads don't mess up each
other's work. And a lock is something that allows
a thread to make sure that when it is
accessing some particular piece of shared data It is
not inter being, being interfered with by some other
thread. That's the purpose of a lock. So the
idea would be that, a thread would acquire a
lock, and once it acquires a lock, it knows
that It can access this data that it shares
with potentially other threads. I'm showing only two threads
here, but potentially in a multi-threaded program you can
have a lot more threads that are sharing a data structure.
And once T1 knows that it has access to this data
structure, then it can do whatever it wants with it. And
then once it is done with whatever it wants to do
with this data it can release the lock. So that's sort
of the idea behind a lock. And locks come in two
flavors, one is what we'll call an exclusive lock, or a
mutual exclusion lock. And this is exactly the one that you
implemented in your first project. And the idea
is, as the name suggests, a mutually exclusive lock
means that it can be used by a thread, one thread at a time. That's the idea.
And here's a silly example of two children
playing, and you know, they have to take turns
in order to hit this ball and obviously,
you don't want both of them hitting the ball
at the same time. Not good for the game and not good for the safety of the
children either. That same, same thing that applies
to the mutual exclusion lock that we use in
parallel programming. The idea is that a thread
that wants to modify data It has to make
sure that when it is modifying the data, nobody
else is going to be accessing that particular data structure.
And therefore it is going to get a mutual exclusion
lock, it knows that nobody else is going to be messing
with it. Then it can modify the data and then
release the lock. And similarly if another thread wants to
read that data and wants the assurance that nobody is
going to be modifying this data while it is reading it,
it can get a, an exclusive lock, access the data,
read it and then release it. That's the idea behind mutually
exclusive lock. You can also have a shared lock.
Now, what that means is that this lock is something
that allows multiple threats to access the data at the
same time. Well, under what conditions would that me meaningful?
Well, here is a, an analogy again. If there
is a newspaper, and multiple people want to read the newspaper
at the same time, perfectly fine to do that, right?
That's the same sort of thing that happens often in
parallel programming. That you have a database, and, and there
are records in the database that multiple threads want to
inspect. But they want to make sure that while they're inspecting
it the, the data itself is not going to be changed so
a shared lock is something that allows multiple readers to
access some data with the assurance that nobody else is
going to be modifying the data. So these are two different
types of locks that you might have that might be useful
in developing multi-threaded shared memory programs.