简体   繁体   中英

Variant of Dekker's algorithm confusion

This program execute two different threads and tell me who the winner of the "race" is.

Unexpectedly sometimes BOTH threads "wins" (I expected someone or no one to win). Is this expected behaviour and why? I'm obviously missing something fundamental here.

class Program
{
    public volatile static int a = 0; 
    public volatile static int b = 0;

    public static void Main()
    {
        for(int i = 0; i < 1000; i++)
        {
            a = 0; 
            b = 0;

            Parallel.Invoke(delegate { a = 1; if (b == 0) Console.WriteLine("A wins"); },
                            delegate { b = 1; if (a == 0) Console.WriteLine("B wins"); });

            Console.WriteLine(System.Environment.NewLine);

            Thread.Sleep(500);
        }
    }
}

Results:

A wins

B wins

A wins
B wins

A wins

...

You're using volatile incorrectly:

declaring the variables volatile is not enough, you need to make sure that everywhere you read/write them, you use Thread.VolatileRead(ref myVar) / Thread.VolatileWrite(ref myVar)

Also, volatile does NOT ensure read/write order (from different threads), even if used correctly. Browse SO for information on the topic. EDIT: it seems to do on a x86 single core machine

You could simply use the lock statement, but if you want to get to the bottom of this, I recommand reading, understanding, then reading again this free e-book

ADDITIONS:
I just browsed through the Parallel class in .NET 4, and nowhere the volatile keyword is used.
They also copy the array of Action<T> before looping over it for some reason, but I doubt that impacts you.

Both Win when they execute in parallel.

From the documentation ( http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.invoke.aspx ): Executes each of the provided actions, possibly in parallel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM