7.3.2 Code Locking
Keywords: perfbook_html

##
7.3.2 Code Locking

Code locking is the simplest locking design, using only global locks.7.6It is especially easy to retrofit an existing program to use code locking in order to run it on a multiprocessor. If the program has only a single shared resource, code locking will even give optimal performance. However, many of the larger and more complex programs require much of the execution to occur in critical sections, which in turn causes code locking to sharply limits their scalability.

Therefore, you should use code locking on programs that spend only a small fraction of their execution time in critical sections or from which only modest scaling is required. In these cases, code locking will provide a relatively simple program that is very similar to its sequential counterpart, as can be seen in Figure . However, not that the simple return of the comparison in hash_search() in Figure has now become three statements due to the need to release the lock before returning.

Figure: Code-Locking Hash Table Search

However, code locking is particularly prone to ``lock contention'', where multiple CPUs need to acquire the lock concurrently. SMP programmers who have taken care of groups of small children (or of older people who are acting like children) will immediately recognize the danger of having only one of something, as illustrated in Figure .

One solution to this problem, named ``data locking'', is described in the next section.

Figure: Lock Contention