10
Art of Multiprocessor Programming10 Coarse-Grained Synchronization Sequential bottleneck –Threads stand in line Adding more threads –Does not improve throughput –Struggle to keep it from getting worse So why even use a multiprocessor? –Well, some apps inherently parallel …

11
Art of Multiprocessor Programming11 This Lecture Introduce four patterns –Bag of tricks … –Methods that work more than once …

12
Art of Multiprocessor Programming12 This Lecture Introduce four patterns –Bag of tricks … –Methods that work more than once … For highly-concurrent objects –Concurrent access –More threads, more throughput

13
Art of Multiprocessor Programming13 First: Fine-Grained Synchronization Instead of using a single lock … Split object into –Independently-synchronized components Methods conflict when they access –The same component … –At the same time

35
Art of Multiprocessor Programming35 Reasoning about Concurrent Objects Invariant –Property that always holds Established because –True when object is created –Truth preserved by each method Each step of each method

55
Art of Multiprocessor Programming55 Coarse-Grained Locking Easy, same as synchronized methods –One lock to rule them all …

56
Art of Multiprocessor Programming56 Coarse-Grained Locking Easy, same as synchronized methods –One lock to rule them all … Simple, clearly correct –Deserves respect! Works poorly with contention –Queue locks help –But bottleneck still an issue

57
Art of Multiprocessor Programming57 Fine-grained Locking Requires careful thought –Do not meddle in the affairs of wizards, for they are subtle and quick to anger

58
Art of Multiprocessor Programming58 Fine-grained Locking Requires careful thought –Do not meddle in the affairs of wizards, for they are subtle and quick to anger Split object into pieces –Each piece has own lock –Methods that work on disjoint pieces need not exclude each other

163
Art of Multiprocessor Programming163 Correctness If –Nodes b and d both locked –Node b still accessible –Node d still successor to b Then –Neither will be deleted –No thread can add c after b –OK to return false

179
Art of Multiprocessor Programming179 On Exit from Loop If item is present –curr holds item –pred just before curr If item is absent –curr has first higher key –pred just before curr Assuming no synchronization problems

188
Art of Multiprocessor Programming188 Evaluation Optimistic is effective if –cost of scanning twice without locks is less than –cost of scanning once with locks Drawback –contains() acquires locks –90% of calls in many apps

243
Art of Multiprocessor Programming243 Changing State Public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark); If this is the current reference … And this is the current mark …

244
Art of Multiprocessor Programming244 Changing State Public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark); …then change to this new reference … … and this new mark

252
Art of Multiprocessor Programming252 Traversing the List Q: what do you do when you find a logically deleted node in your path? A: finish the job. –CAS the predecessors next field –Proceed (repeat as needed)

291
Art of Multiprocessor Programming291 To Lock or Not to Lock Locking vs. Non-blocking: –Extremist views on both sides The answer: nobler to compromise –Example: Lazy list combines blocking add() and remove() and a wait-free contains() –Remember: Blocking/non-blocking is a property of a method

292
Art of Multiprocessor Programming292 This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.Creative Commons Attribution- ShareAlike 2.5 License You are free: –to Share to copy, distribute and transmit the work –to Remix to adapt the work Under the following conditions: –Attribution. You must attribute the work to The Art of Multiprocessor Programming (but not in any way that suggests that the authors endorse you or your use of the work). –Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to –http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.