<

Lab 2: Thread Management

Original author: Munehiro Fukuda

Revisions 2019: Morris Bernstein

Background

The purpose of this lab is to familiarize yourself with the pthread library and pthread synchronization: you will code a program that simulates a game where players pass a ball around.

One way to create a simulation is to create a thread for each independent actor. Actors communicate by sending messages to each other. Using pthreads, information is exchanged by obtaining exclusive access to state variables, then sending a signal1 to the actor's pthread_cond_t condition variable.

The receiving thread waits on the condition variable and then checks its state variables. Usually, this is done inside a while loop because timing may cause spurious signals to be sent.

Check the manual pages for the the following library functions:

The Assignment

The game starts with all players scrambling for the ball. One player gets the ball and dribbles it for a time before passing to another player. The receiving player dribbles and then passes the ball to the next player. Each time a player passes a ball, the turn counter is decremented. The game ends when the turn counter reaches zero.

The players who don't have the ball wait until the ball is passed to them, but they have limited attention spans. They will periodically get bored and stop waiting for the ball before regaining their focus.

A skeleton program lab-02-skel.cc has been provided. Your job is to fill in the missing pieces identified by comments containing the string “TODO(lab)”.

Expected Output

Here is the output from a single run:


Malcolm scrambles for the ballZoe scrambles for the ball

Hoban scrambles for the ball
Inara scrambles for the ball
Jayne scrambles for the ball
Kaylee scrambles for the ball
Simon scrambles for the ball
River scrambles for the ball
Derrial scrambles for the ball
Derrial is distracted for Zoe4
Malcolm wins the scramble is ready to catch the ball

Inara is distracted for 3
Kaylee is ready to catch the ball
ZoeSimon is ready to catch the ball
JayneHoban is ready to catch the ball
 is distracted for 1
 dribbles for River is ready to catch the ball
4
Hoban is ready to catch the ball
Inara is ready to catch the ball
Derrial is ready to catch the ball
Zoe passes to Derrial
Zoe is ready to catch the ball
Derrial catches the ball
Derrial dribbles for 7
Zoe is distracted for 4
Inara is distracted for 5
Derrial passes to Inara
Derrial is distracted for 1
Zoe is ready to catch the ball
Derrial is ready to catch the ball
Hoban is ready to catch the ball
Jayne is ready to catch the ball
Jayne is distracted for 1
Inara is ready to catch the ball
Inara catches the ball
Inara dribbles for 8
River is ready to catch the ball
Jayne is ready to catch the ball
Simon is ready to catch the ball
Malcolm is ready to catch the ball
Derrial is ready to catch the ball
Kaylee is ready to catch the ball
Inara passes to Kaylee
Inara is distracted for 3
Kaylee catches the ball
Kaylee dribbles for 6
Zoe is ready to catch the ball
Inara is ready to catch the ball
Simon is ready to catch the ball
Zoe is ready to catch the ball
Kaylee passes to Hoban
Kaylee is distracted for 3
Hoban catches the ball
Hoban dribbles for 6
River is distracted for 3
Jayne is distracted for 5
Malcolm is distracted for 2
Zoe is distracted for 4
Kaylee is ready to catch the ball
Malcolm is ready to catch the ball
River is ready to catch the ball
Derrial is distracted for 1
Derrial is ready to catch the ball
River is distracted for Jayne is ready to catch the ball2

Hoban passes to Simon
Hoban is ready to catch the ball
Simon catches the ball
Simon dribbles for 7
Zoe is ready to catch the ball
River is ready to catch the ball
Kaylee is ready to catch the ball
Simon passes to River
Simon is distracted for 3
River catches the ball
River dribbles for 7
Jayne is distracted for 5
Simon is ready to catch the ball
Inara is ready to catch the ball
Simon is ready to catch the ball
Malcolm is distracted for 3
Jayne is ready to catch the ball
River passes to Kaylee
River is distracted for 3
Kaylee catches the ball
Kaylee dribbles for 2
Malcolm is ready to catch the ball
Derrial is distracted for 2
Hoban is ready to catch the ball
Simon is ready to catch the ball
Kaylee passes to Malcolm
Kaylee is ready to catch the ball
Malcolm catches the ball
Malcolm dribbles for 3
Derrial is ready to catch the ball
Hoban is ready to catch the ball
River is ready to catch the ball
River is distracted for 4
Malcolm passes to Derrial
Malcolm is ready to catch the ball
Derrial catches the ball
Derrial dribbles for 9
Kaylee is distracted for 4
Zoe is ready to catch the ball
River is ready to catch the ball
Inara is ready to catch the ball
Simon is distracted for 1
Simon is ready to catch the ball
River is distracted for 1
Kaylee is ready to catch the ball
River is ready to catch the ball
Zoe is ready to catch the ball
Derrial passes to Hoban
Derrial is ready to catch the ball
Hoban catches the ball
Hoban dribbles for 1
Hoban passes to Inara
Hoban is distracted for 4
Inara catches the ball
Inara dribbles for 1
Inara passes to Malcolm
Inara is distracted for 1
Malcolm catches the ball
Malcolm dribbles for 6
Inara is ready to catch the ball
Jayne is distracted for 5
Hoban is ready to catch the ball
Malcolm passes to Zoe
Malcolm is ready to catch the ball
Zoe catches the ball
Zoe dribbles for 8
Jayne is ready to catch the ball
Simon is ready to catch the ball
River is ready to catch the ball
Inara is ready to catch the ball
Kaylee is ready to catch the ball
Simon is ready to catch the ball
Inara is distracted for 5
Derrial is distracted for 5
Hoban is distracted for 4
Inara is ready to catch the ball
Hoban is ready to catch the ball
Zoe passes to Simon
Zoe is distracted for 4
Simon catches the ball
Simon dribbles for 2
Derrial is ready to catch the ball
Simon passes to Jayne
Simon is ready to catch the ball
Jayne catches the ball
Jayne dribbles for 5
River is distracted for 5
Hoban is distracted for 3
Zoe is ready to catch the ball
Derrial is ready to catch the ball
Inara is ready to catch the ball
Malcolm is ready to catch the ball
Hoban is ready to catch the ball
Malcolm is ready to catch the ball
Jayne passes to Derrial
Jayne is ready to catch the ball
Derrial catches the ball
Derrial dribbles for 5
River is ready to catch the ball
Kaylee is ready to catch the ball
River is ready to catch the ball
Inara is ready to catch the ball
Derrial passes to Simon
Derrial is ready to catch the ball
Simon catches the ball
Simon dribbles for 10
Zoe is distracted for 5
Jayne is ready to catch the ball
Jayne is ready to catch the ball
Kaylee is ready to catch the ball
Zoe is ready to catch the ball
Simon passes to Zoe
Simon is ready to catch the ball
Zoe catches the ball
Zoe dribbles for 7
Simon is ready to catch the ball
Jayne is ready to catch the ball
Hoban is ready to catch the ball
Malcolm is distracted for 3
Derrial is distracted for 5
River is ready to catch the ball
Malcolm is ready to catch the ball
Zoe passes to Jayne
Zoe is ready to catch the ball
Jayne catches the ball
Jayne dribbles for 8
Inara is distracted for 4
Derrial is ready to catch the ball
Derrial is ready to catch the ball
Kaylee is ready to catch the ball
Malcolm is distracted for 1
Inara is ready to catch the ball
Malcolm is ready to catch the ball
Simon is ready to catch the ball
River is distracted for 1
Jayne passes to Simon
Jayne is ready to catch the ball
Simon catches the ball
Simon dribbles for 2
River is ready to catch the ball
Simon passes to Zoe
Simon is ready to catch the ball
Zoe catches the ball
Zoe dribbles for 3
Hoban is ready to catch the ball
Zoe passes to Inara
Zoe is ready to catch the ball
Inara catches the ball
Inara dribbles for 1
River is ready to catch the ball
Inara passes to Jayne
Inara is distracted for 5
Jayne catches the ball
Jayne dribbles for 3
Hoban is ready to catch the ball
Jayne passes to Derrial
Jayne is distracted for 1
Derrial catches the ball
Derrial dribbles for 7
Kaylee is ready to catch the ball
Jayne is ready to catch the ball
Zoe is distracted for 3
Inara is ready to catch the ball
Kaylee is ready to catch the ball
Zoe is ready to catch the ball
Malcolm is distracted for 1
Malcolm is ready to catch the ball
Derrial passes to Simon
Derrial is distracted for 3
SimonRiver hits the showers
MalcolmZoe hits the showers hits the showers

KayleeHoban hits the showers
 hits the showers hits the showers
Jayne hits the showers

Inara hits the showers
Derrial is ready to catch the ball
Derrial hits the showers
game over

Notes and Hints

A mutex variable controls mutual exclusion, so by definition it limits the ability of the program to do things concurrently. It's good practice to hold a mutex only long enough to read and modify the shared data. All other work (especially print statements which may involve a context switch) should be done outside the critical section.

Several things are happening at once: one player is dribbling the ball, then passing, while other players may become distracted. Since the print statements are not protected, individual threads' output may be interleaved. Without taking special action to prevent it, this is normal behavior.

The interesting part of the output is the linear sequence between the one-at-a-time player who has the ball. Concurrently, other players are getting distracted. To see the main activity sequence, try filtering out the lines containing distracted or ready using grep -v.


Malcolm scrambles for the ballZoe scrambles for the ball

Hoban scrambles for the ball
Inara scrambles for the ball
Jayne scrambles for the ball
Kaylee scrambles for the ball
Simon scrambles for the ball
River scrambles for the ball
Derrial scrambles for the ball

4
Zoe passes to Derrial
Derrial catches the ball
Derrial dribbles for 7
Derrial passes to Inara
Inara catches the ball
Inara dribbles for 8
Inara passes to Kaylee
Kaylee catches the ball
Kaylee dribbles for 6
Kaylee passes to Hoban
Hoban catches the ball
Hoban dribbles for 6

Hoban passes to Simon
Simon catches the ball
Simon dribbles for 7
Simon passes to River
River catches the ball
River dribbles for 7
River passes to Kaylee
Kaylee catches the ball
Kaylee dribbles for 2
Kaylee passes to Malcolm
Malcolm catches the ball
Malcolm dribbles for 3
Malcolm passes to Derrial
Derrial catches the ball
Derrial dribbles for 9
Derrial passes to Hoban
Hoban catches the ball
Hoban dribbles for 1
Hoban passes to Inara
Inara catches the ball
Inara dribbles for 1
Inara passes to Malcolm
Malcolm catches the ball
Malcolm dribbles for 6
Malcolm passes to Zoe
Zoe catches the ball
Zoe dribbles for 8
Zoe passes to Simon
Simon catches the ball
Simon dribbles for 2
Simon passes to Jayne
Jayne catches the ball
Jayne dribbles for 5
Jayne passes to Derrial
Derrial catches the ball
Derrial dribbles for 5
Derrial passes to Simon
Simon catches the ball
Simon dribbles for 10
Simon passes to Zoe
Zoe catches the ball
Zoe dribbles for 7
Zoe passes to Jayne
Jayne catches the ball
Jayne dribbles for 8
Jayne passes to Simon
Simon catches the ball
Simon dribbles for 2
Simon passes to Zoe
Zoe catches the ball
Zoe dribbles for 3
Zoe passes to Inara
Inara catches the ball
Inara dribbles for 1
Inara passes to Jayne
Jayne catches the ball
Jayne dribbles for 3
Jayne passes to Derrial
Derrial catches the ball
Derrial dribbles for 7
Derrial passes to Simon
SimonRiver hits the showers
MalcolmZoe hits the showers hits the showers

KayleeHoban hits the showers
 hits the showers hits the showers
Jayne hits the showers

Inara hits the showers
Derrial hits the showers
game over

All instances of the Player object are held privately in static data of the class but the play() method needs to be public. Why?

TODO comments not marked TODO(lab) are just comments about potential future improvements to the code. They are not part of the lab exercise, but may help understanding how the simulation could evolve.

If you're feeling ambitious, modify the print statements to include a timestamp, so the transcript will show the real-time delays between messages.

If you have to print debugging messages to trace what is happening, use the standard error stream. Output from standard errors can be filtered separately from standard output using simple shell syntax.

A more complex simulation might include the ball-carrying player choosing to pass or throw the ball at the basket. A basket throw would be a score or a miss. Scores would be tallied. After a throw, there would be another scramble for the ball.

Footnotes