thread synchronization
DESCRIPTION
Thread Synchronization. Tutorial #8 CPSC 261. A thread is a virtual processor. Each thread is provided the illusion that it owns a core Copy of the registers It is running all the time Programs involving threads tend to have to solve some synchronization problems. The problems. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/1.jpg)
Thread Synchronization
Tutorial #8CPSC 261
![Page 2: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/2.jpg)
A thread is a virtual processor
• Each thread is provided the illusion that it owns a core– Copy of the registers– It is running all the time
• Programs involving threads tend to have to solve some synchronization problems
![Page 3: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/3.jpg)
The problems
• The bounded buffer that we have talked about is an example of a “classic” synchronization problem
• Also known as the producer/consumer problem
![Page 4: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/4.jpg)
Producer/ConsumerProducers Consumers
send() receive()
Bounded buffer
![Page 5: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/5.jpg)
Producer/Consumer constraints
• Each message sent by a producer is received by exactly one consumer
• Each message received by a consumer was sent by a producer
• There are never more than N messages in the bounded buffer (the bound is N)
• Liveness – the system “makes progress”
![Page 6: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/6.jpg)
Readers/Writers
Resource
Readers Writers
read() write()
![Page 7: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/7.jpg)
Readers/Writers constraints
• There can be either k readers (k >= 1) or 1 writer using the resource at any instant in time
![Page 8: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/8.jpg)
Variation #1
• Reader priority– No reader waits any longer than absolutely
necessary
![Page 9: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/9.jpg)
Variation #2
• Writer priority– No writer waits any longer than absolutely
necessary
![Page 10: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/10.jpg)
Variation #3
• Fairness– The order in which threads first attempt to access
the resource determines the order in which readers/writers get access to the resource
– No thread will starve – eventually each thread will access the resource no matter what other threads exist or what they do
![Page 11: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/11.jpg)
Sleeping Barber
Barber
WaitingRoom
Customers
![Page 12: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/12.jpg)
Sleeping Barber constraints
• The barber cuts one customer’s hair at a time• When the barber finishes cutting hair:– The first arriving customer moves to the barber
chair and gets his hair cut– If there are no customers, the barber goes to sleep
![Page 13: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/13.jpg)
Sleeping Barber constraints II
• Customers arrive at arbitrary times:– If the barber chair is empty the customer sits in
the barber chair• If the barber is sleeping, he wakes up and gets to work
– If the barber chair is full, the customer sits in an empty chair in the waiting room
– If there are no empty chairs the customer just leaves
![Page 14: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/14.jpg)
Sleeping Barber constraints III
• Liveness• Fairness
![Page 15: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/15.jpg)
Dining Philosophers
![Page 16: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/16.jpg)
Dining philosophers constraints
• Never-emptying plate of spaghetti on the table• 5 forks on the table one between each pair of
philosophers• Each philosopher needs 2 forks to eat spaghetti– They will only use the forks on their immediate left
and immediate right• Each philosopher:– Think, eat, think, eat, ...
• Philosophers don’t care about hygiene
![Page 17: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/17.jpg)
Variation #1
• The classical problem:• Left then right– The philosopher first picks up the fork on his
immediate left– Then picks up the fork on his immediate right– Then eats– Then puts down both forks (order doesn’t matter)
![Page 18: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/18.jpg)
Variation #2
• The resource ordering problem:• There is a total order on forks (they are
numbered 0 – 4)– The philosopher first picks up the fork with the
lower number (among his left and right forks)– Then picks up the fork with the higher number– Then eats– Then puts down both forks (order doesn’t matter)
![Page 19: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/19.jpg)
Variation #3
• The random problem:– The philosopher first randomly picks up either his
left fork or his right fork• If it isn’t on the table, he gives up and starts over
– Then picks up the other fork• If it isn’t on the table, he gives up, puts down the first
fork, and starts over– Then eats– Then puts down both forks (order doesn’t matter)
![Page 20: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/20.jpg)
Your task
• Implement these 3 variations of the dining philosophers problem
![Page 21: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/21.jpg)
Your tools
• Pthreads– 1 thread for each philosopher
• Pthreads Mutexes• Pthreads condition variables• Semaphores
![Page 22: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/22.jpg)
Pthreads mutexes
• created as all other objects (malloc)• initialized by pthread_mutex_init()• acquired by pthread_mutex_lock()• released by pthread_mutex_unlock()
![Page 23: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/23.jpg)
Pthreads condition variables
• created as all other objects (malloc)• initialized by pthread_cond_init()• wait by pthread_cond_wait()– wait always occurs in a loop, checking the
condition each time around• signal by pthread_cond_signal()
![Page 24: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/24.jpg)
Pthreads semaphores
• created as all other objects (malloc)• initialized by sem_init()• wait by sem_wait()• post by sem_post()
![Page 25: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/25.jpg)
Hints
• Think a little bit about data structures:– How are you going to represent each fork?– What thread synchronization constructs are you
going to need?• How are you going to convince yourself (and
eventually me and the TAs) that your solution is correct?– Print trace messages about what is going on
![Page 26: Thread Synchronization](https://reader036.vdocument.in/reader036/viewer/2022081417/56815c6a550346895dca7d89/html5/thumbnails/26.jpg)
A sample trace$ ./phil-ordered1: got left fork2: got left fork2: got right fork2: eat2: put back right fork2: put back left fork3: got left fork0: got left fork2: got left fork3: got right fork3: eat3: put back right fork3: put back left fork...