Thread Synchronization

26
Thread Synchronization Tutorial #8 CPSC 261

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 Presentation

Transcript of Thread Synchronization

Page 1: Thread Synchronization

Thread Synchronization

Tutorial #8CPSC 261

Page 2: Thread Synchronization

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

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

Producer/ConsumerProducers Consumers

send() receive()

Bounded buffer

Page 5: Thread Synchronization

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

Readers/Writers

Resource

Readers Writers

read() write()

Page 7: Thread Synchronization

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

Variation #1

• Reader priority– No reader waits any longer than absolutely

necessary

Page 9: Thread Synchronization

Variation #2

• Writer priority– No writer waits any longer than absolutely

necessary

Page 10: Thread Synchronization

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

Sleeping Barber

Barber

WaitingRoom

Customers

Page 12: Thread Synchronization

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

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

Sleeping Barber constraints III

• Liveness• Fairness

Page 15: Thread Synchronization

Dining Philosophers

Page 16: Thread Synchronization

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

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

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

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

Your task

• Implement these 3 variations of the dining philosophers problem

Page 21: Thread Synchronization

Your tools

• Pthreads– 1 thread for each philosopher

• Pthreads Mutexes• Pthreads condition variables• Semaphores

Page 22: Thread Synchronization

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

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

Pthreads semaphores

• created as all other objects (malloc)• initialized by sem_init()• wait by sem_wait()• post by sem_post()

Page 25: Thread Synchronization

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

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...