我试图修改著名的“Dekker's algorithm”,以便您可以同时使用三个进程这是我的代码:

package DekkersAlgorithm;

class DekkerAlg {
    /* Iterations done by each Thread */
    static final int iterations = 2000000;
    /* Shared variable */
    static volatile int sharedInteger = 0;
    /* P Thread for critical section */
    static volatile boolean wantp = false;
    /* Q Thread for critical section */
    static volatile boolean wantq = false;
    /* Z Thread for critical section */
    static volatile boolean wantz = false;
    /* Thread turn */
    static volatile int turn = 1;

class P extends Thread {
    public void run() {
        for (int i=0; i<iterations; ++i) {
            /* No critical section */
            wantp = true;
            while (wantq || wantz) {
                if (turn == 2) {
                    wantp = false;
                    while (turn == 2)
                        Thread.yield();
                    wantp = true;
                }
            }

            /* Critical section */
            ++sharedInteger;
            /* End critical section */

            turn = 2;
            wantp = false;
        }
    }
}

class Q extends Thread {
    public void run() {
        for (int i=0; i<iterations; ++i) {
            /* No critical section */
            wantq = true;
            while (wantp || wantz) {
                if (turn == 1) {
                    wantq = false;
                    while (turn == 1)
                        Thread.yield();
                    wantq = true;
                }
            }

            /* Critical section */
            --sharedInteger;
            /* End critical section */

            turn = 1;
            wantq = false;
        }
    }
}

class Z extends Thread {
    public void run() {
        for (int i=0; i<iterations; ++i) {
            /* No critical section */
            wantz = true;
            while (wantp || wantq) {
                if (turn == 3) {
                    wantz = false;
                    while (turn == 3)
                        Thread.yield();
                    wantz = true;
                }
            }

            /* Critical section */
            ++sharedInteger;
            /* End critical section */

            turn = 3;
            wantz = false;
        }
    }
}

DekkerAlg() {
    Thread p = new P();
    Thread q = new Q();
    Thread z = new Z();
    p.start();
    q.start();
    z.start();

    try {
        p.join();
        q.join();
        z.join();
        System.out.println("The value of the sharedInteger is " + sharedInteger);
        System.out.println("It should be different from 0.");
    }
    catch (InterruptedException e) {}
}

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

它在低迭代次数下运行良好,但是当我将这个变量设置为500(+)时,程序有时无法完成。我认为alivelock发生在最后两个Threads之间,但我需要一个关于如何解决它的线索。
你能帮帮我吗?

最佳答案

我认为你没有适当地延长。在dekker中,它意味着如果两个人都想进入他们的cs,那么谁就可以进入;这里它似乎意味着如果其他人想进入他们的cs,那么谁就必须等待。对于2个过程来说,它们是直接对立的;对于3个过程来说,就不那么多了。
一种方法是有一个进程列表,指定如果对CS有争用,谁必须等待谁这样,如果P - Q想要进入,Z刚刚退出,Z将被移到列表的结尾,所以你有一个方法在P - Q之间选择(如果你能用一个方法来表示这个“列表”的话,它的修改可能是原子的,这是可行的,因为只有6种不同的模式来表示,那就更好了!)

08-04 04:32