Open In App

Dekker’s algorithm in Process Synchronization

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisite – Process Synchronization, Inter Process Communication 
To obtain such a mutual exclusion, bounded waiting, and progress there have been several algorithms implemented, one of which is Dekker’s Algorithm. To understand the algorithm let’s understand the solution to the critical section problem first. 
A process is generally represented as : 
 

do {
//entry section
critical section
//exit section
remainder section
} while (TRUE);



The solution to the critical section problem must ensure the following three conditions: 

  1. Mutual Exclusion
  2. Progress
  3. Bounded Waiting


One of the solutions for ensuring above all factors is Peterson’s solution.
Another one is Dekker’s Solution. Dekker’s algorithm was the first probably-correct solution to the critical section problem. It allows two threads to share a single-use resource without conflict, using only shared memory for communication. It avoids the strict alternation of a naïve turn-taking algorithm, and was one of the first mutual exclusion algorithms to be invented.
Although there are many versions of Dekker’s Solution, the final or 5th version is the one that satisfies all of the above conditions and is the most efficient of them all. 
Note – Dekker’s Solution, mentioned here, ensures mutual exclusion between two processes only, it could be extended to more than two processes with the proper use of arrays and variables.
Algorithm – It requires both an array of Boolean values and an integer variable:
 

var flag: array [0..1] of boolean;
turn: 0..1;
repeat
flag[i] := true;
while flag[j] do
if turn = j then
begin
flag[i] := false;
while turn = j do no-op;
flag[i] := true;
end;
critical section
turn := j;
flag[i] := false;
remainder section
until false;


First Version of Dekker’s Solution – The idea is to use a common or shared thread number between processes and stop the other process from entering its critical section if the shared thread indicates the former one already running.

CPP
Main()
{

    int thread_number = 1;
    startThreads();
}

Thread1()
{
    do {

        // entry section
        // wait until threadnumber is 1
        while (threadnumber == 2)
            ;

        // critical section

        // exit section
        // give access to the other thread
        threadnumber = 2;

        // remainder section

    } while (completed == false)
}

Thread2()
{

    do {

        // entry section
        // wait until threadnumber is 2
        while (threadnumber == 1)
            ;

        // critical section

        // exit section
        // give access to the other thread
        threadnumber = 1;

        // remainder section

    } while (completed == false)
}
Java
// Java program for the above approach
import java.lang.Thread;

public class ThreadExample {

    static boolean completed = false;
    static int threadNumber = 1;
    static void Thread1()
    {
        boolean doWhile = false;
        while (!completed || !doWhile) {
            doWhile = true;

            // entry section wait until
            // threadNumber is 1
            while (threadNumber == 2) {
                Thread.yield();
            }

            // critical section

            // exit section give access
            // to the other thread
            threadNumber = 2;

            // remainder section
        }
    }

    static void Thread2()
    {
        boolean doWhile = false;
        while (!completed || !doWhile) {
            doWhile = true;

            // entry section wait until
            // threadNumber is 2
            while (threadNumber == 1) {
                Thread.yield();
            }

            // critical section

            // exit section give access
            // to the other thread
            threadNumber = 1;

            // remainder section
        }
    }

    static void StartThreads()
    {
        Thread t1 = new Thread(ThreadExample::Thread1);
        Thread t2 = new Thread(ThreadExample::Thread2);
        t1.start();
        t2.start();
    }

    // Driver Code
    public static void main(String[] args)
    {
        threadNumber = 1;
        StartThreads();
    }
}
C#
using System.Threading;

class ThreadExample {
    static bool completed = false;
    static int threadNumber = 1;

    static void Thread1()
    {
        bool doWhile = false;
        while (!completed || !doWhile) {
            doWhile = true;
            // entry section
            // wait until threadNumber is 1
            while (threadNumber == 2) {
                Thread.Yield();
            }

            // critical section

            // exit section
            // give access to the other thread
            threadNumber = 2;

            // remainder section
        }
    }

    static void Thread2()
    {
        bool doWhile = false;
        while (!completed || !doWhile) {
            doWhile = true;
            // entry section
            // wait until threadNumber is 2
            while (threadNumber == 1) {
                Thread.Yield();
            }

            // critical section

            // exit section
            // give access to the other thread
            threadNumber = 1;

            // remainder section
        }
    }

    static void StartThreads()
    {
        Thread t1 = new Thread(Thread1);
        Thread t2 = new Thread(Thread2);
        t1.Start();
        t2.Start();
    }

    static void Main(string[] args)
    {
        threadNumber = 1;
        StartThreads();
    }
}

// This code is contributed by Shivhack999
Javascript
let thread_number = 1;

function Thread1() {
    let doWhile = false;
    while (!completed || !doWhile) {
        doWhile = true;
        // entry section
        // wait until threadnumber is 1
        while (thread_number === 2) {
            // pass
        }

        // critical section

        // exit section
        // give access to the other thread
        thread_number = 2;

        // remainder section
    }
}

function Thread2() {
    let doWhile = false;
    while (!completed || !doWhile) {
        doWhile = true;
        // entry section
        // wait until threadnumber is 2
        while (thread_number === 1) {
            // pass
        }

        // critical section

        // exit section
        // give access to the other thread
        thread_number = 1;

        // remainder section
    }
}

startThreads();
Python3
def Thread1():
    doWhile=False
    while not completed or not doWhile:
        doWhile=True
        # entry section
        # wait until threadnumber is 1
        while (threadnumber == 2):
            pass

        # critical section

        # exit section
        # give access to the other thread
        threadnumber = 2

        # remainder section

def Thread2():
    doWhile=False
    while not completed or not doWhile:
        doWhile=True
        # entry section
        # wait until threadnumber is 2
        while (threadnumber == 1):
            pass

        # critical section

        # exit section
        # give access to the other thread
        threadnumber = 1

        # remainder section

if __name__ == '__main__':

    thread_number = 1
    startThreads()

The problem arising in the above implementation is lockstep synchronization, i.e each thread depends on the other for its execution. If one of the processes completes, then the second process runs, gives access to the completed one, and waits for its turn, however, the former process is already completed and would never run to return the access back to the latter one. Hence, the second process waits infinitely then.
Second Version of Dekker’s Solution – To remove lockstep synchronization, it uses two flags to indicate its current status and updates them accordingly at the entry and exit section.

CPP
Main()
{

    // flags to indicate if each thread is in
    // its critical section or not.
    boolean thread1 = false;
    boolean thread2 = false;

    startThreads();
}

Thread1()
{

    do {

        // entry section
        // wait until thread2 is in its critical section
        while (thread2 == true)
            ;

        // indicate thread1 entering its critical section
        thread1 = true;

        // critical section

        // exit section
        // indicate thread1 exiting its critical section
        thread1 = false;

        // remainder section

    } while (completed == false)
}

Thread2()
{

    do {

        // entry section
        // wait until thread1 is in its critical section
        while (thread1 == true)
            ;

        // indicate thread2 entering its critical section
        thread2 = true;

        // critical section

        // exit section
        // indicate thread2 exiting its critical section
        thread2 = false;

        // remainder section

    } while (completed == false)
}
Java
public class MutualExclusion {

    // flags to indicate if each thread is in
    // its critical section or not
    boolean thread1 = false;
    boolean thread2 = false;

    // method to start the threads
    public void startThreads() {
        new Thread(new Runnable() {
            public void run() {
                thread1();
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                thread2();
            }
        }).start();
    }

    public void thread1() {
        do {
            // entry section
            // wait until thread2 is in its critical section
            while (thread2 == true);

            // indicate thread1 entering its critical section
            thread1 = true;

            // critical section

            // exit section
            // indicate thread1 exiting its critical section
            thread1 = false;

            // remainder section
        } while (completed == false);
    }

    public void thread2() {
        do {
            // entry section
            // wait until thread1 is in its critical section
            while (thread1 == true);

            // indicate thread2 entering its critical section
            thread2 = true;

            // critical section

            // exit section
            // indicate thread2 exiting its critical section
            thread2 = false;

            // remainder section
        } while (completed == false);
    }

    public static void main(String[] args) {
        MutualExclusion me = new MutualExclusion();
        me.startThreads();
    }
}
C#
using System;
using System.Threading;

class Program
{
    static bool thread1 = false;
    static bool thread2 = false;
    static bool completed = false;

    static void Main(string[] args)
    {
        // Start both threads
        Thread threadOne = new Thread(new ThreadStart(Thread1));
        Thread threadTwo = new Thread(new ThreadStart(Thread2));

        threadOne.Start();
        threadTwo.Start();
    }

    static void Thread1()
    {
        do
        {
            // entry section
            // wait until thread2 is in its critical section
            while (thread2)
            {
                // Spin-wait
            }

            // indicate thread1 entering its critical section
            thread1 = true;

            // critical section

            // exit section
            // indicate thread1 exiting its critical section
            thread1 = false;

            // remainder section
        }
        while (!completed);
    }

    static void Thread2()
    {
        do
        {
            // entry section
            // wait until thread1 is in its critical section
            while (thread1)
            {
                // Spin-wait
            }

            // indicate thread2 entering its critical section
            thread2 = true;

            // critical section

            // exit section
            // indicate thread2 exiting its critical section
            thread2 = false;

            // remainder section
        }
        while (!completed);
    }
}
Javascript
let thread1InCriticalSection = false;
let thread2InCriticalSection = false;

function thread1() {
  // entry section
  Promise.resolve().then(() => {
    // wait until thread2 is in its critical section
    while (thread2InCriticalSection) {}

    // indicate thread1 entering its critical section
    thread1InCriticalSection = true;

    // critical section

    // exit section
    // indicate thread1 exiting its critical section
    thread1InCriticalSection = false;

    // remainder section
    thread1();
  });
}

function thread2() {
  // entry section
  Promise.resolve().then(() => {
    // wait until thread1 is in its critical section
    while (thread1InCriticalSection) {}

    // indicate thread2 entering its critical section
    thread2InCriticalSection = true;

    // critical section

    // exit section
    // indicate thread2 exiting its critical section
    thread2InCriticalSection = false;

    // remainder section
    thread2();
  });
}

thread1(); // start thread1
thread2(); // start thread2
Python3
def Thread1():
    doWhile=False
    while not completed or not doWhile:
        doWhile=True
        # entry section
        # wait until thread2 is in its critical section
        while (thread2):
            pass

        # indicate thread1 entering its critical section
        thread1 = True

        # critical section

        # exit section
        # indicate thread1 exiting its critical section
        thread1 = False

        # remainder section

def Thread2():
    doWhile=False
    while not completed or not doWhile:
        doWhile=True
        # entry section
        # wait until thread1 is in its critical section
        while (thread1):
            pass

        # indicate thread1 entering its critical section
        thread2 = True

        # critical section

        # exit section
        # indicate thread2 exiting its critical section
        thread2 = False

        # remainder section
        
if __name__ == '__main__':

    # flags to indicate if each thread is in
    # its critical section or not.
    thread1 = False
    thread2 = False

    startThreads()

The problem arising in the above version is mutual exclusion itself. If threads are preempted (stopped) during flag updation ( i.e during current_thread = true ) then, both the threads enter their critical section once the preempted thread is restarted, also the same can be observed at the start itself, when both the flags are false.
Third Version of Dekker’s Solution – To re-ensure mutual exclusion, it sets the flags before the entry section itself.
 

C++
Main()
{

    // flags to indicate if each thread is in
    // queue to enter its critical section
    boolean thread1wantstoenter = false;
    boolean thread2wantstoenter = false;

    startThreads();
}

Thread1()
{

    do {

        thread1wantstoenter = true;

        // entry section
        // wait until thread2 wants to enter
        // its critical section
        while (thread2wantstoenter == true)
            ;

        // critical section

        // exit section
        // indicate thread1 has completed
        // its critical section
        thread1wantstoenter = false;

        // remainder section

    } while (completed == false)
}

Thread2()
{

    do {

        thread2wantstoenter = true;

        // entry section
        // wait until thread1 wants to enter
        // its critical section
        while (thread1wantstoenter == true)
            ;

        // critical section

        // exit section
        // indicate thread2 has completed
        // its critical section
        thread2wantstoenter = false;

        // remainder section

    } while (completed == false)
}
Java
class SharedData {
    static boolean completed = false;
}

class Thread1 extends Thread {
    private static boolean thread1wantstoenter = false;
    private static boolean thread2wantstoenter = false;

    @Override
    public void run() {
        do {
            thread1wantstoenter = true;

            // Entry section
            // Wait until Thread2 wants to enter its critical section
            while (thread2wantstoenter)
                ;

            // Critical section
            System.out.println("Thread1 is in the critical section");

            // Exit section
            // Indicate Thread1 has completed its critical section
            thread1wantstoenter = false;

            // Remainder section
        } while (!SharedData.completed);
    }
}

class Thread2 extends Thread {
    private static boolean thread1wantstoenter = false;
    private static boolean thread2wantstoenter = false;

    @Override
    public void run() {
        do {
            thread2wantstoenter = true;

            // Entry section
            // Wait until Thread1 wants to enter its critical section
            while (thread1wantstoenter)
                ;

            // Critical section
            System.out.println("Thread2 is in the critical section");

            // Exit section
            // Indicate Thread2 has completed its critical section
            thread2wantstoenter = false;

            // Remainder section
        } while (!SharedData.completed);
    }
}

public class Main {
    public static void main(String[] args) {
        SharedData.completed = false;

        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        thread1.start();
        thread2.start();

        // Assuming some condition when threads should stop
        // For demonstration purposes, let's say after a certain time
        try {
            Thread.sleep(5000); // Sleep for 5 seconds
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        SharedData.completed = true; // Stop the threads
    }
}
C#
using System;
using System.Threading;

class Program
{
    private static AutoResetEvent thread1Event = new AutoResetEvent(false);
    private static AutoResetEvent thread2Event = new AutoResetEvent(false);
    private static bool completed = false;

    static void Main()
    {
        StartThreads();
    }

    static void StartThreads()
    {
        // Create threads for Thread1 and Thread2
        Thread t1 = new Thread(Thread1);
        Thread t2 = new Thread(Thread2);

        // Start the threads
        t1.Start();
        t2.Start();

        // Wait for both threads to complete or a timeout
        t1.Join(5000); // Set a timeout (adjust as needed)
        t2.Join(5000); // Set a timeout (adjust as needed)

        // If threads do not complete within the timeout, consider alternative handling
        if (t1.IsAlive || t2.IsAlive)
        {
            Console.WriteLine("Threads did not complete within the timeout.");
            // Perform additional handling or force termination as needed
        }
        else
        {
            Console.WriteLine("Both threads completed.");
        }
    }

    static void Thread1()
    {
        do
        {
            // Entry section
            thread1Event.Set();

            // Wait until Thread2 wants to enter its critical section or completion
            if (!completed)
            {
                thread2Event.WaitOne();
            }

            // Critical section

            // Exit section
            // Indicate Thread1 has completed its critical section
            thread1Event.Reset();

            // Remainder section

        } while (!completed);
    }

    static void Thread2()
    {
        do
        {
            // Entry section
            thread2Event.Set();

            // Wait until Thread1 wants to enter its critical section or completion
            if (!completed)
            {
                thread1Event.WaitOne();
            }

            // Critical section

            // Exit section
            // Indicate Thread2 has completed its critical section
            thread2Event.Reset();

            // Remainder section

        } while (!completed);
    }
}
Javascript
let thread1wantstoenter = false;
let thread2wantstoenter = false;
let completed = false; // Variable to indicate if both threads have completed

// Function to simulate thread execution
async function runThread(threadName, wantstoenter, otherThreadWantsToEnter) {
    do {
        wantstoenter = true;

        while (otherThreadWantsToEnter) {
            // Wait for a random amount of time
            await wait(100); // Simulating random wait time
        }

        // Entry section
        // Wait until the other thread wants to enter its critical section

        // Critical section
        console.log(`${threadName} is in the critical section`);

        // Exit section
        // Indicate the current thread has completed its critical section
        wantstoenter = false;

        // Remainder section
        console.log(`${threadName} is executing the remainder section`);

    } while (!completed);
}

// Function to simulate waiting
function wait(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// Start both threads
async function startThreads() {
    await Promise.all([
        runThread('Thread1', thread1wantstoenter, thread2wantstoenter),
        runThread('Thread2', thread2wantstoenter, thread1wantstoenter)
    ]);
}

// Start the threads
startThreads().then(() => {
    console.log("Both threads have completed their execution");
});
Python3
if __name__=='__main__':
    # flags to indicate if each thread is in
    # queue to enter its critical section
    thread1wantstoenter = False
    thread2wantstoenter = False

    startThreads()


def Thread1():
    doWhile=False
    while (completed == False or not doWhile):
        doWhile=True
        thread1wantstoenter = True

        # entry section
        # wait until thread2 wants to enter
        # its critical section
        while (thread2wantstoenter == True):
            pass
            

        # critical section

        # exit section
        # indicate thread1 has completed
        # its critical section
        thread1wantstoenter = False

        # remainder section


def Thread2():
    doWhile=False
    while (completed == False or not doWhile) :
        doWhile=True
        thread2wantstoenter = True

        # entry section
        # wait until thread1 wants to enter
        # its critical section
        while (thread1wantstoenter == True):
            pass
            

        # critical section

        # exit section
        # indicate thread2 has completed
        # its critical section
        thread2wantstoenter = False

        # remainder section

The problem with this version is a deadlock possibility. Both threads could set their flag as true simultaneously and both will wait infinitely later on.
Fourth Version of Dekker’s Solution – Uses small time interval to recheck the condition, eliminates deadlock, and ensures mutual exclusion as well.
 

CPP
Main()
{

    // flags to indicate if each thread is in
    // queue to enter its critical section
    boolean thread1wantstoenter = false;
    boolean thread2wantstoenter = false;

    startThreads();
}

Thread1()
{

    do {

        thread1wantstoenter = true;

        while (thread2wantstoenter == true) {

            // gives access to other thread
            // wait for random amount of time
            thread1wantstoenter = false;

            thread1wantstoenter = true;
        }

        // entry section
        // wait until thread2 wants to enter
        // its critical section

        // critical section

        // exit section
        // indicate thread1 has completed
        // its critical section
        thread1wantstoenter = false;

        // remainder section

    } while (completed == false)
}

Thread2()
{

    do {

        thread2wantstoenter = true;

        while (thread1wantstoenter == true) {

            // gives access to other thread
            // wait for random amount of time
            thread2wantstoenter = false;

            thread2wantstoenter = true;
        }

        // entry section
        // wait until thread1 wants to enter
        // its critical section

        // critical section

        // exit section
        // indicate thread2 has completed
        // its critical section
        thread2wantstoenter = false;

        // remainder section

    } while (completed == false)
}
Java
public class TwoThreadMutex {

    // flags to indicate if each thread wants to
    // enter its critical section
    private static boolean thread1wantstoenter = false;
    private static boolean thread2wantstoenter = false;

    public static void main(String[] args) {

        startThreads();
    }

    private static void startThreads() {
        Thread t1 = new Thread(TwoThreadMutex::Thread1);
        Thread t2 = new Thread(TwoThreadMutex::Thread2);

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void Thread1() {

        do {

            thread1wantstoenter = true;

            while (thread2wantstoenter == true) {

                // gives access to other thread
                // wait for random amount of time
                thread1wantstoenter = false;

                thread1wantstoenter = true;
            }

            // entry section
            // wait until thread2 wants to enter
            // its critical section

            // critical section

            // exit section
            // indicate thread1 has completed
            // its critical section
            thread1wantstoenter = false;

            // remainder section

        } while (completed == false);
    }

    private static void Thread2() {

        do {

            thread2wantstoenter = true;

            while (thread1wantstoenter == true) {

                // gives access to other thread
                // wait for random amount of time
                thread2wantstoenter = false;

                thread2wantstoenter = true;
            }

            // entry section
            // wait until thread1 wants to enter
            // its critical section

            // critical section

            // exit section
            // indicate thread2 has completed
            // its critical section
            thread2wantstoenter = false;

            // remainder section

        } while (completed == false);
    }
}
C#
using System;
using System.Threading;

class Program
{
    static object lockObject = new object(); // Lock object for synchronization
    static bool completed = false;

    static void Main()
    {
        // Start both threads
        Thread thread1 = new Thread(Thread1);
        Thread thread2 = new Thread(Thread2);

        thread1.Start();
        thread2.Start();

        // Wait for both threads to complete
        thread1.Join();
        thread2.Join();
    }

    static void Thread1()
    {
        bool doWhile = false;
        while (!completed || !doWhile)
        {
            doWhile = true;
            lock (lockObject)
            {
                // Entry section
                Monitor.Wait(lockObject); // Wait until signaled by Thread2
                // Critical section
                Console.WriteLine("Thread1 is in critical section");
                completed = true; // Indicate completion
                // Exit section
                Monitor.Pulse(lockObject); // Notify Thread2
            }
            // Remainder section
        }
    }

    static void Thread2()
    {
        bool doWhile = false;
        while (!completed || !doWhile)
        {
            doWhile = true;
            lock (lockObject)
            {
                // Entry section
                Monitor.Wait(lockObject); // Wait until signaled by Thread1
                // Critical section
                Console.WriteLine("Thread2 is in critical section");
                completed = true; // Indicate completion
                // Exit section
                Monitor.Pulse(lockObject); // Notify Thread1
            }
            // Remainder section
        }
    }
}
Javascript
let thread1wantstoenter = false;
let thread2wantstoenter = false;
let completed = false; // Variable to indicate if both threads have completed

function startThreads() {
    // Start both threads
    Thread1();
    Thread2();
}

function Thread1() {
    do {
        thread1wantstoenter = true;

        while (thread2wantstoenter) {
            // Gives access to other thread
            // Wait for a random amount of time
            thread1wantstoenter = false;
            thread1wantstoenter = true;
        }

        // Entry section
        // Wait until Thread2 wants to enter its critical section

        // Critical section

        // Exit section
        // Indicate Thread1 has completed its critical section
        thread1wantstoenter = false;

        // Remainder section

    } while (!completed);
}

function Thread2() {
    do {
        thread2wantstoenter = true;

        while (thread1wantstoenter) {
            // Gives access to other thread
            // Wait for a random amount of time
            thread2wantstoenter = false;
            thread2wantstoenter = true;
        }

        // Entry section
        // Wait until Thread1 wants to enter its critical section

        // Critical section

        // Exit section
        // Indicate Thread2 has completed its critical section
        thread2wantstoenter = false;

        // Remainder section

    } while (!completed);
}

// Start the threads
startThreads();
Python3
if __name__ == '__main__':

    # flags to indicate if each thread is in
    # queue to enter its critical section
    thread1wantstoenter = False
    thread2wantstoenter = False

    startThreads()


def Thread1():
    doWhile=False
    while (completed == False or not doWhile):
        doWhile=True
        thread1wantstoenter = True

        while (thread2wantstoenter == True) :

            # gives access to other thread
            # wait for random amount of time
            thread1wantstoenter = False

            thread1wantstoenter = True
        

        # entry section
        # wait until thread2 wants to enter
        # its critical section

        # critical section

        # exit section
        # indicate thread1 has completed
        # its critical section
        thread1wantstoenter = False

        # remainder section

    


def Thread2():
    doWhile=False
    while (completed == False or not doWhile):
        doWhile=True
        thread2wantstoenter = True

        while (thread1wantstoenter == True) :

            # gives access to other thread
            # wait for random amount of time
            thread2wantstoenter = False

            thread2wantstoenter = True
        

        # entry section
        # wait until thread1 wants to enter
        # its critical section

        # critical section

        # exit section
        # indicate thread2 has completed
        # its critical section
        thread2wantstoenter = False

        # remainder section

The problem with this version is the indefinite postponement. Also, a random amount of time is erratic depending upon the situation in which the algorithm is being implemented, hence not an acceptable solution in business critical systems.
Dekker’s Algorithm: Final and completed Solution – -Idea is to use favoured thread notion to determine entry to the critical section. Favoured thread alternates between the thread providing mutual exclusion and avoiding deadlock, indefinite postponement, or lockstep synchronization.
 

CPP
Main()
{

    // to denote which thread will enter next
    int favouredthread = 1;

    // flags to indicate if each thread is in
    // queue to enter its critical section
    boolean thread1wantstoenter = false;
    boolean thread2wantstoenter = false;

    startThreads();
}

Thread1()
{
    do {

        thread1wantstoenter = true;

        // entry section
        // wait until thread2 wants to enter
        // its critical section
        while (thread2wantstoenter == true) {

            // if 2nd thread is more favored
            if (favaouredthread == 2) {

                // gives access to other thread
                thread1wantstoenter = false;

                // wait until this thread is favored
                while (favouredthread == 2)
                    ;

                thread1wantstoenter = true;
            }
        }

        // critical section

        // favor the 2nd thread
        favouredthread = 2;

        // exit section
        // indicate thread1 has completed
        // its critical section
        thread1wantstoenter = false;

        // remainder section

    } while (completed == false)
}

Thread2()
{

    do {

        thread2wantstoenter = true;

        // entry section
        // wait until thread1 wants to enter
        // its critical section
        while (thread1wantstoenter == true) {

            // if 1st thread is more favored
            if (favaouredthread == 1) {

                // gives access to other thread
                thread2wantstoenter = false;

                // wait until this thread is favored
                while (favouredthread == 1)
                    ;

                thread2wantstoenter = true;
            }
        }

        // critical section

        // favour the 1st thread
        favouredthread = 1;

        // exit section
        // indicate thread2 has completed
        // its critical section
        thread2wantstoenter = false;

        // remainder section

    } while (completed == false)
}
Java
public class Main {
    static int favouredThread = 1;
    static boolean thread1WantsToEnter = false;
    static boolean thread2WantsToEnter = false;
    static boolean completed = false; // Declaration of the shared variable

    public static void main(String[] args) {
        startThreads();
    }

    static void startThreads() {
        Thread thread1 = new Thread(() -> Thread1());
        Thread thread2 = new Thread(() -> Thread2());

        thread1.start();
        thread2.start();
    }

    static void Thread1() {
        do {
            thread1WantsToEnter = true;
            while (thread2WantsToEnter) {
                if (favouredThread == 2) {
                    thread1WantsToEnter = false;
                    while (favouredThread == 2);
                    thread1WantsToEnter = true;
                }
            }
            favouredThread = 2;
            thread1WantsToEnter = false;
        } while (!completed);
    }

    static void Thread2() {
        do {
            thread2WantsToEnter = true;
            while (thread1WantsToEnter) {
                if (favouredThread == 1) {
                    thread2WantsToEnter = false;
                    while (favouredThread == 1);
                    thread2WantsToEnter = true;
                }
            }
            favouredThread = 1;
            thread2WantsToEnter = false;
        } while (!completed);
    }
}
C#
using System;
using System.Threading;

public class MainClass
{
    // to denote which thread will enter next
    private static volatile int favouredThread = 1;

    // flags to indicate if each thread is in
    // queue to enter its critical section
    private static volatile bool thread1WantsToEnter = false;
    private static volatile bool thread2WantsToEnter = false;

    private static volatile bool completed = false; // Assume completed is a boolean variable

    public static void Main(string[] args)
    {
        Thread t1 = new Thread(Thread1);
        Thread t2 = new Thread(Thread2);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();

        Console.WriteLine("Both threads have completed.");
    }

    private static void Thread1()
    {
        bool doWhile = false;
        while (!completed || !doWhile)
        {
            doWhile = true;
            thread1WantsToEnter = true;

            // entry section
            // wait until thread2 wants to enter
            // its critical section
            while (thread2WantsToEnter)
            {
                // if 2nd thread is more favored
                if (favouredThread == 2)
                {
                    // gives access to other thread
                    thread1WantsToEnter = false;

                    // wait until this thread is favored
                    while (favouredThread == 2)
                    {
                        // Busy wait
                    }

                    thread1WantsToEnter = true;
                }
            }

            // critical section

            // favor the 2nd thread
            favouredThread = 2;

            // exit section
            // indicate thread1 has completed
            // its critical section
            thread1WantsToEnter = false;

            // remainder section
        }
    }

    private static void Thread2()
    {
        bool doWhile = false;
        while (!completed || !doWhile)
        {
            doWhile = true;
            thread2WantsToEnter = true;

            // entry section
            // wait until thread1 wants to enter
            // its critical section
            while (thread1WantsToEnter)
            {
                // if 1st thread is more favored
                if (favouredThread == 1)
                {
                    // gives access to other thread
                    thread2WantsToEnter = false;

                    // wait until this thread is favored
                    while (favouredThread == 1)
                    {
                        // Busy wait
                    }

                    thread2WantsToEnter = true;
                }
            }

            // critical section

            // favour the 1st thread
            favouredThread = 1;

            // exit section
            // indicate thread2 has completed
            // its critical section
            thread2WantsToEnter = false;

            // remainder section
        }
    }
}
Python3
if __name__ == '__main__':

    # to denote which thread will enter next
    favouredthread = 1

    # flags to indicate if each thread is in
    # queue to enter its critical section
    thread1wantstoenter = False
    thread2wantstoenter = False

    startThreads()


def Thread1():
    doWhile=False
    while (completed == False or not doWhile) :
        doWhile=True
        thread1wantstoenter = True

        # entry section
        # wait until thread2 wants to enter
        # its critical section
        while (thread2wantstoenter == True) :

            # if 2nd thread is more favored
            if (favaouredthread == 2) :

                # gives access to other thread
                thread1wantstoenter = False

                # wait until this thread is favored
                while (favouredthread == 2):
                    pass
                    

                thread1wantstoenter = True
            
        

        # critical section

        # favor the 2nd thread
        favouredthread = 2

        # exit section
        # indicate thread1 has completed
        # its critical section
        thread1wantstoenter = False

        # remainder section

     


def Thread2():
    doWhile=False
    while (completed == False or not doWhile) :
        doWhile=True
        thread2wantstoenter = True

        # entry section
        # wait until thread1 wants to enter
        # its critical section
        while (thread1wantstoenter == True) :

            # if 1st thread is more favored
            if (favaouredthread == 1) :

                # gives access to other thread
                thread2wantstoenter = False

                # wait until this thread is favored
                while (favouredthread == 1):
                    pass
                    

                thread2wantstoenter = True
            
        

        # critical section

        # favour the 1st thread
        favouredthread = 1

        # exit section
        # indicate thread2 has completed
        # its critical section
        thread2wantstoenter = False

        # remainder section

     

This version guarantees a complete solution to the critical solution problem.
References – 
Dekker’s Algorithm -csisdmz.ul.ie 
Dekker’s algorithm – Wikipedia
 



Last Updated : 16 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads