2

I know how looks dekker algorithm but why something like that would not work?

static void Enter(int i)
    {
        int j = 1 - i;
        flags[i] = true;
        if(flags[j])
        {
            while (turn!=i)
            {                    
            }
        }
    }
    static void Exit(int i)
    {
        flags[i] = false;
        turn = 1 - i;
    }

What is wrong with my solution?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • You can't use Dekker on modern machines, it is not able to cope with multicore cpus. They require a memory barrier for proper synchronization. – Hans Passant Apr 07 '13 at 17:50
  • I know it. I read just about Dekker and I wonder why dekker could not be simplified to above code? – user2204936 Apr 07 '13 at 17:52
  • There are lots of ways to simplify an algorithm that doesn't work. Producing another one that doesn't work so is equal to the original. Clearly your question doesn't make much sense, does it? – Hans Passant Apr 07 '13 at 17:54
  • Let assume that modern machines don't do out of order execution. Let do the same assumtion what Dekker did. – user2204936 Apr 07 '13 at 17:55

1 Answers1

0

There are ways of simplyfing the Dekker's algorithm, for example the Peterson's algorithm, to which your code is very similar. According to wiki, Dekker's was the first correct solution of the problem, so no wonder he didn't get the simplest code on the first try.

After a short look, your code seems to work correctly, but that cannot be guaranteed without a formal proof.

regnarg
  • 676
  • 8
  • 10
  • I can prove it doesn't work. What happens if one of the threads never calls Enter() [but might have on other input]? – Joshua Jul 27 '16 at 17:56