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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

138 CHAPTER 13. EASE OF USEFigure 13.1:Wikipedia)Mandelbrot Set (Courtesy ofbeing created and deleted frequently. 2 Another approachwould be to use a hash table and to lock theindividual hash buckets, but this can perform poorlywhen scanning the list in order.A third approach is to lock the individual list elements,and to require the locks for both the predecessorand successor to be held during the insertion.Since both locks must be acquired, we need to decidewhich order to acquire them in. Two conventionalapproaches would be to acquire the locks inaddress order, or to acquire them in the order thatthey appear in the list, so that the header is alwaysacquired first when it is one of the two elements beinglocked. However, both of these methods requirespecial checks and branches.The to-be-shaven solution is to unconditionallyacquire the locks in list order. But what about deadlock?Deadlock cannot occur.To see this, number the elements in the list startingwith zero for the header up to N for the lastelement in the list (the one preceding the header,given that the list is circular). Similarly, numberthe threads from zero to N − 1. <strong>If</strong> each thread attemptsto lock some consecutive pair of elements, atleast one of the threads is guaranteed to be able toacquire both locks.Why?Because there are not enough threads to reach allthe way around the list. Suppose thread 0 acquireselement 0’s lock. To be blocked, some other threadmust have already acquired element 1’s lock, so letus assume that thread 1 has done so. Similarly, forthread 1 to be blocked, some other thread must haveacquired element 2’s lock, and so on, up throughthread N − 1, who acquires element N − 1’s lock.For thread N −1 to be blocked, some other threadmust have acquired element N’s lock. But there areno more threads, and so thread N − 1 cannot beblocked. Therefore, deadlock cannot occur.<strong>So</strong> why should we prohibit use of this delightfullittle algorithm?Thefactisthatifyoureally wanttouseit, wecannotstop you. We can, however, recommend againstsuch code being included in any project that we careabout.But, before you use this algorithm, please thinkthrough the following Quick Quiz.Quick Quiz 13.1: <strong>Can</strong> a similar algorithm beused when deleting elements?The fact is that this algorithm is extremely specialized(it only works on certain sized lists), andalso quite fragile. Any bug that accidentally failedto add a node to the list could result in deadlock.In fact, simply adding the node a bit too late couldresult in deadlock.In addition, the other algorithms described aboveare “good and sufficient”. For example, simply acquiringthe locks in address order is fairly simple andquick, while allowing the use of lists of any size. Justbe careful of the special cases presented by emptylists and lists containing only one element!Quick Quiz 13.2: Yetch!!! <strong>What</strong> ever possessedsomeone to come up with an algorithm that deservesto be shaved as much as this one does???In summary, we do not use algorithms simply becausethey happen to work. We instead restrict ourselvesto algorithms that are useful enough to makeit worthwhile learning about them. The more difficultand complex the algorithm, the more generallyuseful it must be in order for the pain of learning itand fixing its bugs to be worthwhile.Quick Quiz 13.3: Give an exception to this rule.Exceptions aside, we must continue to shave thesoftware “Mandelbrot set” so that our programs remainmaintainable, as shown in Figure 13.2.2 Those of you with strong operating-system backgrounds,please suspend disbelief. <strong>If</strong> you are unable to suspend disbelief,send us a better example.

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

Saved successfully!

Ooh no, something went wrong!