10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

294 APPENDIX F. ANSWERS TO QUICK QUIZZESAnswer:This is a very simple hash table with no chaining,so the only element in a given bucket is the firstelement. The reader is invited to adapt this exampleto a hash table with full chaining.Quick Quiz 6.2:<strong>What</strong> race condition can occur in Figure 6.1?Answer:Consider the following sequence of events:1. Thread 0 invokes delete(0), and reachesline 10 of the figure, acquiring the lock.2. Thread 1 concurrently invokes delete(0), andreaches line 10, but spins on the lock becauseThread 1 holds it.3. Thread 0 executes lines 11-14, removing the elementfrom the hashtable, releasing the lock,and then freeing the element.4. Thread 0 continues execution, and allocatesmemory, gettingtheexactblockofmemorythatit just freed.5. Thread 0 then initializes this block of memoryas some other type of structure.6. Thread 1’s spin_lock() operation fails due tothe fact that what it believes to be p->lock isno longer a spinlock.Because there is no existence guarantee, the identityof the data element can change while a thread isattempting to acquire that element’s lock on line 10!F.7 Chapter 8: Deferred ProcessingQuick Quiz 8.1:Why not implement reference-acquisition usinga simple compare-and-swap operation that onlyacquires a reference if the reference counter isnon-zero?Answer:Although this can resolve the race between therelease of the last reference and acquisition ofa new reference, it does absolutely nothing toprevent the data structure from being freed andreallocated, possibly as some completely differenttype of structure. <strong>It</strong> is quite likely that the “simplecompare-and-swap operation” would give undefinedresults if applied to the differently typed structure.In short, use of atomic operations such ascompare-and-swap absolutely requires either typesafetyor existence guarantees.Quick Quiz 8.2:Why isn’t it necessary to guard against cases whereone CPU acquires a reference just after anotherCPU releases the last reference?Answer:Because a CPU must already hold a reference inorder to legally acquire another reference. Therefore,if one CPU releases the last reference, therecannot possibly be any CPU that is permitted toacquire a new reference. This same fact allows thenon-atomic check in line 22 of Figure 8.2.Quick Quiz 8.3:<strong>If</strong> the check on line 22 of Figure 8.2 fails, how couldthe check on line 23 possibly succeed?Answer:Suppose that kref put() is protected by RCU, sothat two CPUs might be executing line 22 concurrently.Both might see the value “2”, causing bothto then execute line 23. One of the two instances ofatomic dec and test() will decrement the valueto zero and thus return 1.Quick Quiz 8.4:How can it possibly be safe to non-atomically checkfor equality with “1” on line 22 of Figure 8.2?Answer:Remember that it is not legal to call eitherkref get() or kref put() unless you hold a reference.<strong>If</strong> the reference count is equal to “1”, thenthere can’t possibly be another CPU authorized tochange the value of the reference count.Quick Quiz 8.5:Why can’t the check for a zero reference count bemade in a simple “if” statement with an atomicincrement in its “then” clause?Answer:Suppose that the “if” condition completed, finding

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!