Dekker's algorithm is the first known correct solution to themutual exclusion problem inconcurrent programming where processes only communicate via shared memory. The solution was attributed toDutchmathematicianTh. J. Dekker byEdsger W. Dijkstra in an unpublished paper on sequential process descriptions[1] and his manuscript oncooperating sequential processes.[2] It allows two threads to share a single-use resource without conflict, using onlyshared memory for communication.
It avoids the strict alternation of a naive turn-taking algorithm, and was one of the firstmutual exclusion algorithms to be invented.
If two processes attempt to enter acritical section at the same time, the algorithm will allow only one process in, based on whoseturn it is. If one process is already in the critical section, the other process willbusy wait for the first process to exit. This is done by the use of two flags,wants_to_enter[0] andwants_to_enter[1], which indicate an intention to enter the critical section on the part of processes 0 and 1, respectively, and a variableturn that indicates who has priority between the two processes.

Dekker's algorithm can be expressed inpseudocode, as follows.[3]
variables wants_to_enter : array of 2 booleans turn : integer wants_to_enter[0] ← false wants_to_enter[1] ← false turn ← 0 // or 1 | |
p0: wants_to_enter[0] ← true while wants_to_enter[1] { if turn ≠ 0 { wants_to_enter[0] ← false while turn ≠ 0 { // busy wait } wants_to_enter[0] ← true } } // critical section ... turn ← 1 wants_to_enter[0] ← false // remainder section | p1: wants_to_enter[1] ← true while wants_to_enter[0] { if turn ≠ 1 { wants_to_enter[1] ← false while turn ≠ 1 { // busy wait } wants_to_enter[1] ← true } } // critical section ... turn ← 0 wants_to_enter[1] ← false // remainder section |
Processes indicate an intention to enter the critical section which is tested by the outer while loop. If the other process has not flagged intent, the critical section can be entered safely irrespective of the current turn. Mutual exclusion will still be guaranteed as neither process can become critical before setting their flag (implying at least one process will enter the while loop). This also guarantees progress as waiting will not occur on a process which has withdrawn intent to become critical. Alternatively, if the other process's variable was set, the while loop is entered and the turn variable will establish who is permitted to become critical. Processes without priority will withdraw their intention to enter the critical section until they are given priority again (the inner while loop). Processes with priority will break from the while loop and enter their critical section.
Dekker's algorithm guaranteesmutual exclusion, freedom fromdeadlock, and freedom fromstarvation. Let us see why the last property holds. Suppose p0 is stuck inside thewhile wants_to_enter[1] loop forever. There is freedom from deadlock, so eventually p1 will proceed to its critical section and setturn = 0 (and the value of turn will remain unchanged as long as p0 doesn't progress). Eventually p0 will break out of the innerwhile turn ≠ 0 loop (if it was ever stuck on it). After that it will setwants_to_enter[0] to true and settle down to waiting forwants_to_enter[1] to become false (sinceturn = 0, it will never do the actions in the while loop). The next time p1 tries to enter its critical section, it will be forced to execute the actions in itswhile wants_to_enter[0] loop. In particular, it will eventually setwants_to_enter[1] to false and get stuck in thewhile turn ≠ 1 loop (since turn remains 0). The next time control passes to p0, it will exit thewhile wants_to_enter[1] loop and enter its critical section.
If the algorithm were modified by performing the actions in thewhile wants_to_enter[1] loop without checking ifturn = 0, then there is a possibility of starvation. Thus all the steps in the algorithm are necessary.
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(May 2015) (Learn how and when to remove this message) |
One advantage of this algorithm is that it doesn't require specialtest-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 ofbusy waiting instead of process suspension. (The use of busy waiting suggests that processes should spend a minimum amount 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 modernCPUs execute their instructions in an out-of-order fashion; even memory accesses can be reordered (seememory ordering). This algorithm won't work onSMP machines equipped with these CPUs without the use ofmemory 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 variableswants_to_enter[0] andwants_to_enter[1] are never accessed in the loop. It can then remove the writes to those variables from the loop, using a process calledloop-invariant code motion. It would also be possible for many compilers to detect that theturn variable is never modified by the inner loop, and perform a similar transformation, resulting in a potentialinfinite 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, C++, 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 necessarymemory barriers to guarantee in-orderexecution 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 wants_to_enter 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.