is a reus a ble solution to a common coordin a tion problem between multiple thre a ds. They describe: • How thre a ds inter a ct • How sh a red d a t a is a ccessed • How execution is coordin a ted s a fely P a tterns a re a bout structure a nd intent
phore, lock, synchronize, pool, a tomic) tell us how to synchronize. P a tterns expl a in when a nd why. With p a tterns: • Code is e a sier to re a son a bout • Beh a vior is predict a ble • Systems sc a le more s a fely
t a ; consumers t a ke d a t a using a sh a red bu ff er or queue. 2. Re a ders–Writers: Multiple re a ders c a n re a d simult a neously, but writers require exclusive a ccess.
r a tes the gener a tion of work from the processing of work. • One or more thre a ds produce t a sks. • One or more thre a ds consume t a sks. • They oper a te a t di ff erent speeds. • It provide decoupling between producers a nd consumers • S a fe sh a ring of a work bu ff er
rrives continuously • Processing is a synchronous • Producers a nd consumers should not block e a ch other unnecess a rily Ex a mples: • T a sk queues • Network mess a ge h a ndling
d simult a neously • Re a ds do not block other re a ds. • Only one thre a d to write exclusively • Writes block everyone. • High perform a nce for re a d-he a vy worklo a ds • D a t a consistency during writes • Controlled a ccess b a sed on oper a tion type
re much more frequent th a n writes • Sh a red d a t a must rem a in consistent • Exclusive write a ccess is required Ex a mples: • Course C a t a log in a University System • Web server c a che • G a me st a tes • IDE - compiler
with two di ff erent modes of a ccess: • Re a d lock → sh a red • Write lock → exclusive Why do we need it? • With a norm a l lock (synchronized or Reentr a ntLock) only one thre a d c a n a ccess the d a t a a t a time • Re a ders block other re a ders, even though they don’t modify a nything. Blocking re a ders from e a ch other is unnecess a ry a nd slow.
move on a grid: Te a m A a nd Te a m B. There is one sh a red b a ll, a nd only one pl a yer owns the b a ll a t a time. • The b a ll c a rrier tries to re a ch the opponent’s go a l. Other pl a yers move concurrently: te a mm a tes try to block ne a rby enemies or support the b a ll c a rrier, while enemies try to collide with the b a ll c a rrier to ste a l the b a ll.
move one cell per turn in a ny of the 8 directions. 2. Other pl a yers a ct a s moving obst a cles. 3. No two pl a yers m a y occupy the s a me cell unless the move is a v a lid collision with the opposing b a ll c a rrier. 4. If a n enemy collides with the b a ll c a rrier, possession ch a nges. 5. When possession ch a nges, the te a m with the b a ll becomes the a tt a cking te a m. 6. If the b a ll c a rrier re a ches the opponent’s go a l, th a t te a m scores. 7. After a go a l, a ll pl a yers return to their origin a l st a rting positions, a nd the te a m th a t conceded the go a l st a rts with possession of the b a ll.
Concurrent Execution. E a ch pl a yer is upd a ted a s a t a sk once per turn. The g a me submits one t a sk per pl a yer, w a its for a ll t a sks to f inish, resolves moves, a nd then redr a ws the grid. • Mode 2 — Independent Pl a yer Thre a ds. E a ch pl a yer runs a s a n independent thre a d. Pl a yers m a y move a t di ff erent speeds, so some pl a yers m a y move more often th a n others. • Atomic / Lock / Sem a phore: protect b a ll ownership, pl a yer positions, a nd limited- a ccess a re a s. • Producer–Consumer: pl a yer t a sks produce movement events; the UI consumes them a nd redr a ws the grid. • Re a ders–Writers: m a ny pl a yers m a y re a d the grid st a te concurrently, but upd a tes to positions or possession require exclusive a ccess.
ll possession is not locked, two pl a yers m a y ste a l the b a ll a t the s a me time. • If position upd a tes a re not protected, two pl a yers m a y move into the s a me cell. • If movement locks a re a cquired incorrectly, pl a yers m a y block e a ch other forever.
. . . . X . # . X . . . . # . . . . # . T . # . . . . . . X . T = target (data) X = enemy (thread) # = wall (data) . = empty cell (data) Each enemy uses BFS to compute the shortest path to the target, moves one step along that path if the next cell is free, and then recomputes the path on its next turn. So, each enemy repeats this cycle independently: 1. Read the current grid 2. Run BFS from my position to T 3. Get the shortest path 4. Move only one step along that path 5. Sleep briefly 6. Repeat
Computing Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2026 Copyright. These slides can only be used as study material for the class CSC 364 at Cal Poly. They cannot be distributed or used for another purpose. 61