Dekker's Algorithm - Note

Note

One advantage of this algorithm is that it doesn't require special Test-and-set (atomic read/modify/write) instructions and is therefore highly portable between languages and machine architectures. One disadvantage is that it is limited to two processes and makes use of busy waiting instead of process suspension. (The use of busy waiting suggests that processes should spend a minimum of time inside the critical section.)

Modern operating systems provide mutual exclusion primitives that are more general and flexible than Dekker's algorithm. However, in the absence of actual contention between the two processes, the entry and exit from critical section is extremely efficient when Dekker's algorithm is used.

Many modern CPUs execute their instructions in an out-of-order fashion; even memory accesses can be reordered (see memory ordering). This algorithm won't work on SMP machines equipped with these CPUs without the use of memory barriers.

Additionally, many optimizing compilers can perform transformations that will cause this algorithm to fail regardless of the platform. In many languages, it is legal for a compiler to detect that the flag variables flag and flag are never accessed in the loop. It can then remove the writes to those variables from the loop, using a process called Loop-invariant code motion. It would also be possible for many compilers to detect that the turn variable is never modified by the inner loop, and perform a similar transformation, resulting in a potential infinite loop. If either of these transformations is performed, the algorithm will fail, regardless of architecture.

To alleviate this problem, volatile variables should be marked as modifiable outside the scope of the currently executing context. For example, in C# or Java, one would annotate these variables as 'volatile'. Note however that the C/C++ "volatile" attribute only guarantees that the compiler generates code with the proper ordering; it does not include the necessary memory barriers to guarantee in-order execution of that code. C++11 atomic variables can be used to guarantee the appropriate ordering requirements — by default, operations on atomic variables are sequentially consistent so if the flag and turn variables are atomic a naive implementation will "just work". Alternatively, ordering can be guaranteed by the explicit use of separate fences, with the load and store operations using a relaxed ordering.

Read more about this topic:  Dekker's Algorithm

Famous quotes containing the word note:

    The note of the white-throated sparrow, a very inspiriting but almost wiry sound, was first heard in the morning, and with this all the woods rang. This was the prevailing bird in the northern part of Maine. The forest generally was alive with them at this season, and they were proportionally numerous and musical about Bangor. They evidently breed in that State.
    Henry David Thoreau (1817–1862)

    For sounds in winter nights, and often in winter days, I heard the forlorn but melodious note of a hooting owl indefinitely far; such a sound as the frozen earth would yield if struck with a suitable plectrum, the very lingua vernacula of Walden Wood, and quite familiar to me at last, though I never saw the bird while it was making it.
    Henry David Thoreau (1817–1862)

    And songs climb out of the flames of the near campfires,
    Pale, pastel things exquisite in their frailness
    With a note or two to indicate it isn’t lost,
    On them at least. The songs decorate our notion of the world
    And mark its limits, like a frieze of soap-bubbles.
    John Ashbery (b. 1927)