[CS61C FA20] Lecture 34.4 - Thread-Level Parallelism II: Synchronization — Transcript
Full transcript
- 0:00and welcome back in this final lecture
- 0:03in this series we're going to learn
- 0:04about
- 0:04how to synchronize this how do we
- 0:06prevent that race condition we saw
- 0:08in the last video synchronization
- 0:12how can we have some software maybe even
- 0:14hardware support for that
- 0:16so the idea is i've got a single
- 0:19re-shared resource and this by the way
- 0:20this is true for any parallel system
- 0:23this isn't just wall it's in c and
- 0:24openmp and how to do this no
- 0:26any in life in computational thinking
- 0:28you've got some resource
- 0:29that's being trying to be requested be
- 0:32used by parallel workers parallel actors
- 0:35how do you limit access to that parallel
- 0:37resource in a way that won't give you
- 0:39the wrong value won't do the wrong thing
- 0:42for example how do you prevent two
- 0:44people from editing the same editing
- 0:45of one file at the same time uh this is
- 0:48true
- 0:49dropbox you know i log into two
- 0:51different laptops and i try to modify
- 0:53the same file
- 0:54in different ways this one i write a
- 0:55this one i write b i say they're both
- 0:56files what happens how does dropbox
- 0:58handle that
- 0:59a dropbox handler says there's a
- 1:01conflicted copy so that
- 1:03is a way to tell me that there's no do i
- 1:04get a notification no but the point is
- 1:06there's a
- 1:06way for them to deal with the shared
- 1:08resource so this is true for you know
- 1:10the
- 1:10writ large in some sense of the of any
- 1:12share any paralyzed system
- 1:15otherwise things can get mixed up um
- 1:17here's a solution
- 1:18just take turns maybe if i'm in dropbox
- 1:21if i'm
- 1:22opening a file and editing it i can't
- 1:23even open it now it certainly lets me
- 1:25but i can say you know what you can't
- 1:26open this file somebody else has the
- 1:27file open so maybe that means when you
- 1:28open a file you might you write a secret
- 1:30thing
- 1:30or something that says this file's open
- 1:32and when you try to open a file every os
- 1:34would look to see if that secret thing
- 1:35is set and therefore it says you can't
- 1:36open it now what happens when there's a
- 1:37raise to that
- 1:38so there's all this conversation how do
- 1:40you prevent the race to this
- 1:41both of us exactly the same time go grab
- 1:44it they both looked and
- 1:45so there's a whole issue of how to
- 1:46prevent that but we've got to think
- 1:47about synchronization in a deeper way
- 1:50for example all controlling your
- 1:51microphone and trying to talk at once so
- 1:53someone gets the microphone and once
- 1:54they grab it they have to release it
- 1:56it's a good practice for kind of
- 1:57controlling uh conversations in
- 1:59classrooms as well
- 2:00so the word for use in the parallel
- 2:02distributed computing world is called a
- 2:04lock
- 2:05and we have to lock out we use a lock to
- 2:07prevent
- 2:08other elements from accessing a shared
- 2:11resource
- 2:12um so this is also by the way known as a
- 2:14semaphore so semaphores unlocks are both
- 2:16usually used in the same way
- 2:17and the idea for this and we'll just see
- 2:20if i have a variable called a lock
- 2:22when it's set to zero it's unlocked when
- 2:24it's set to one it's locked
- 2:25okay so if ever one it means it's locked
- 2:27and someone has control of it okay
- 2:29let's do it let's play with it let's
- 2:30actually code this in c here we go
- 2:32so i have a little lope little loop that
- 2:34says while the lock is not
- 2:36zero so while i'm waiting for the lock
- 2:38to be while
- 2:39while the lock is is being taken i'm
- 2:42just going to sit and spin this is
- 2:43called spin waiting so i'm spin waiting
- 2:45for lock
- 2:46to be released well lock is not zero so
- 2:48i'm going to sit
- 2:49do nothing while lock is set to one or
- 2:51or grabbed
- 2:52okay now it's zero the moment it's
- 2:54released i'm unlocked and what i want to
- 2:56do
- 2:56i one of the threads i'm gonna grab it
- 2:59i'm gonna set the lock
- 3:00lock equals one now i've got the lock
- 3:02whatever that share resource
- 3:04is maybe here it's pi the summation i'm
- 3:06going to say now let me do
- 3:07i may contribute my stuff to pi so i do
- 3:09my shared resource
- 3:11and i that's all sequential by the way
- 3:13because only one person is
- 3:14accessing that at one time but that's
- 3:16okay if the other guy other 99 999
- 3:18people are still computing their stuff i
- 3:20grab the lock nobody's looking nobody
- 3:22needs to write to pie great there's no
- 3:23loss of time really because nobody's
- 3:25trying to grab it if somebody else
- 3:26supposed to go oh i went to grab it now
- 3:27i'm just waiting it's been waiting just
- 3:28like i was been waiting before
- 3:31i contribute my con my contribution into
- 3:33pi i kind of put my my value into the
- 3:35final
- 3:36accumulator and then i have to release
- 3:38the lock so the next person can do that
- 3:39so lock equals zero okay that's what i'm
- 3:41doing
- 3:42so i spin weight until the lock is free
- 3:44it's zero i grab it set it to one
- 3:46do my stuff and then i release it that's
- 3:48this kind of this kind of code you're
- 3:49gonna see
- 3:50all throughout now let's see how this
- 3:52might work i've got two different
- 3:53threads
- 3:54you all run in the same you know the
- 3:56same stream of execution so they both do
- 3:58this
- 3:59one spins awaits the next one spins and
- 4:01waits now here's the problem
- 4:05this the thread two finds the lock is
- 4:08not set
- 4:09before thread one sets it i talked about
- 4:12before
- 4:13how do you handle the fact that these
- 4:14guys come out of order and in fact
- 4:16how much time thread one got versus
- 4:18thread two you know thread one
- 4:20did one thing and then releases it and
- 4:22then goes here so
- 4:24in the time watch this this is this is
- 4:25by the way just make sure you understand
- 4:27this is time which that means
- 4:31this guy was spin waiting until it
- 4:33became free
- 4:35then okay it went to go slow motion i'm
- 4:38going to
- 4:39set lock one
- 4:42in that slow motion time thread two
- 4:44jumped in there
- 4:45it was spin waiting and it saw that it
- 4:48was free
- 4:48now they're both going can you feel it
- 4:51the race condition to go
- 4:52set it they both thread one wakes up
- 4:55sets it to one
- 4:56thread one thinks i've got control of
- 4:58the lock
- 4:59thread two thinks the same thing
- 5:04and they both now have control of the
- 5:06lock so that little code that i showed
- 5:08in the previous slide which looked
- 5:09really clean and really good you know
- 5:11spin weight for it then set it do my
- 5:13private work on it
- 5:14and then release it stan it doesn't seem
- 5:17like there was anything wrong with that
- 5:18except when they interleave in a way
- 5:20that two different threads
- 5:22thought they were waiting for it they
- 5:23both wake up and they both
- 5:25grab for the look and they both said it
- 5:29and they both think that they controlled
- 5:30it what's going to happen is you're
- 5:32going to lose one of those values
- 5:33each one of those is going to contribute
- 5:34their stuff into some and one is going
- 5:36to be overwritten by the other guy
- 5:38that contribution is lost now this is
- 5:40trouble
- 5:41this is real trouble so
- 5:44this is an issue both believe both
- 5:46threads believe they got the lock
- 5:48and by the way try as you like there's
- 5:50no solution to this you know as you try
- 5:52to hit that bullseye
- 5:53you're going to continue well let's what
- 5:55if you had two locks and one grabs
- 5:57no i don't care how clever you're going
- 5:59to be we can't solve this at this
- 6:01at this level we can't solve this go
- 6:04ahead good luck
- 6:04i'll wait pause the video go ahead i'll
- 6:07wait
- 6:08it's not going to happen you're not
- 6:09going to find a solution c to solve this
- 6:11problem
- 6:12this is deeper it's deeper there's
- 6:14something more fundamental we have to
- 6:15add
- 6:15at a lower level so we have to add some
- 6:17new instructions
- 6:18only way to do that we'll see that in
- 6:20the next lecture so
- 6:22last slide on this series i'm very
- 6:23excited this is the second part of tlp
- 6:25tlp2 openmp we love
- 6:28very simple parallel extension to see
- 6:30like how many more lines like an include
- 6:32line and in a parallel
- 6:33you know pragma and you've got a
- 6:35parallelization we love it go play with
- 6:37it
- 6:37stop this video and start playing with
- 6:39this that's wonderful all you do is say
- 6:40parallel forward there
- 6:42c is easy to learn uh not very high
- 6:44level so it's easy to get in trouble
- 6:46so by living in c rather than say going
- 6:48to go and trying to do the same thing
- 6:49try to compute you know parallel try to
- 6:52try to add up uh some values of pi
- 6:54in go and see what explore how how
- 6:56different different or difficult or easy
- 6:57it is to do that
- 6:58compared to doing it doing it in c here
- 7:00with with the openmp
- 7:02we also saw that race conditions are an
- 7:04issue we try to introduce locks and
- 7:05semi-fours as a way to do that but we
- 7:07saw that we can do at the sea level
- 7:09we have to go even deeper to the
- 7:10assembly level to be able to work with
- 7:12that so we need some new instructions
- 7:13we're going to see how to do that in the
- 7:14next
- 7:15next series of lectures okay we'll see
- 7:17you then
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 34.4 - Thread-Level Parallelism II: Synchronization by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,620 words across 253 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.