locked
What is the MOST DIFFERENCES among them? RRS feed

  • Question

  • Hi all——

    For multiple threads sharing the same variable, we can use volatile variable and Thread.VolatileRead/Write, Thread.MemoryBarrier() and InterLock.Add/Decrease()……

    Tell me each of them's function and each give me an example let me know how to use?And example MUST BE OF MOST DIFFERENCE!

    Many thanks Don't refer words from MSDN so easily……

    Sunday, July 22, 2012 7:01 AM

Answers

  • Hi Ignoredrei,

    >>But when I change "100000" to "100", it returns me 0?????

    No, it can also return the other random numbers, please try this way:

            static void Main(string[] args)
            {
                int sum = 0;
                Int32 count = 100;
                for (int j = 0; j < count*1000
                    ; j++)
                {
                    Thread th = new Thread(() =>
                    {
                        for (int i = 1; i < count; i++)
                        {
                            Thread.VolatileWrite(ref sum, sum - 1);
                        }
                    }) { Name = "Thread"+j };
                    th.Start();
                    for (int i = 1; i < count; i++)
                    {
                        Thread.VolatileWrite(ref sum, sum + 1);
                    }
    
                    th.Join();
                   if (sum!=0) Console.WriteLine(sum);
                }
                Console.ReadLine();
            }

    The method VolatileWrite cannot sync the two different threads, Please keep in mind that the volatile modifier does not force synchronization of loads and stores; instead it simply tells the compiler not to change the order of accesses to the field. By eliminating reordering optimizations, the code becomes more predictable from a programmer's perspective. As the documentation said: VolatileRead and VolatileWrite are for special cases of synchronization. Under normal circumstances, the C# lock statement, the Visual Basic SyncLock statement, and the Monitor class provide easier alternatives.

    I hope this will be clear.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Mike Feng Monday, July 30, 2012 7:06 AM
    • Marked as answer by TimoYang Tuesday, July 31, 2012 9:23 AM
    Thursday, July 26, 2012 11:09 AM

All replies

  • Hi Ignoredrei,

    Welcome to the MSDN Forum.

    Based on your description, I assume you have read the documents about these terms on MSDN documents, so I will try to tell the difference with my own words:

    You know, volatile keyword is used for a class-level or struct variable, for local variable, we can use MemoryBarrier, or VolatileRead/write.

    VolatileRead/write includes MemoryBarrier, for example, this is the code snippet for a int variable:

    //Just a sample to understand the VolatileWrite function
            public static void VolatileWrite(ref int address, int value)
            {
                Thread.MemoryBarrier(); address = value;
            }

    And this:

    //Just an example to understand VolatileRead method
            public static int VolatileRead(ref int address)
            {
                int num = address; Thread.MemoryBarrier(); return num;
            }

    When you call VolatileWrite  first, and then call VolatileRead, there is no memorybarrier, and the sequence may be changed between read and write, so you can use interlocked.add, but interlocked.add provides an atomic operation, it is not for a single statement, you can consider it as a small "SyncLock endSyncLock" block.

    There are some blogs for your reference: 

    http://msdn.microsoft.com/en-us/magazine/cc163726.aspx

    http://msdn.microsoft.com/en-us/magazine/cc163467.aspx

    http://msdn.microsoft.com/en-us/magazine/cc817398.aspx

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 23, 2012 7:47 AM
  • So I don't understand why I shouldn't use volatile variable when using multiple-threads?Sample——The result isn't 0?!But when I change "100000" to "100", it returns me 0?????

     class Program
        {
            static void Main(string[] args)
            {
                int sum = 0;
                Thread th = new Thread(() => 
                {
                    for (int i = 1; i <100000; i++)
                    {
                        Thread.VolatileWrite(ref sum, sum - 1);
                    }
                });
                th.Start();
                for (int i =1; i < 100000; i++)
                {
                    Thread.VolatileWrite(ref sum, sum + 1);
                }
     
                th.Join();
                Console.WriteLine(sum);
            }
        }

    • Edited by TimoYang Thursday, July 26, 2012 8:12 AM
    Thursday, July 26, 2012 8:11 AM
  • Hi Ignoredrei,

    >>But when I change "100000" to "100", it returns me 0?????

    No, it can also return the other random numbers, please try this way:

            static void Main(string[] args)
            {
                int sum = 0;
                Int32 count = 100;
                for (int j = 0; j < count*1000
                    ; j++)
                {
                    Thread th = new Thread(() =>
                    {
                        for (int i = 1; i < count; i++)
                        {
                            Thread.VolatileWrite(ref sum, sum - 1);
                        }
                    }) { Name = "Thread"+j };
                    th.Start();
                    for (int i = 1; i < count; i++)
                    {
                        Thread.VolatileWrite(ref sum, sum + 1);
                    }
    
                    th.Join();
                   if (sum!=0) Console.WriteLine(sum);
                }
                Console.ReadLine();
            }

    The method VolatileWrite cannot sync the two different threads, Please keep in mind that the volatile modifier does not force synchronization of loads and stores; instead it simply tells the compiler not to change the order of accesses to the field. By eliminating reordering optimizations, the code becomes more predictable from a programmer's perspective. As the documentation said: VolatileRead and VolatileWrite are for special cases of synchronization. Under normal circumstances, the C# lock statement, the Visual Basic SyncLock statement, and the Monitor class provide easier alternatives.

    I hope this will be clear.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Mike Feng Monday, July 30, 2012 7:06 AM
    • Marked as answer by TimoYang Tuesday, July 31, 2012 9:23 AM
    Thursday, July 26, 2012 11:09 AM
  • >>The method VolatileWrite cannot sync the two different threads, it is designed for processor.

    So when should I use Volatile?

    Saturday, July 28, 2012 3:34 AM
  • >>The method VolatileWrite cannot sync the two different threads, it is designed for processor.

    So when should I use Volatile?

    Hi Ignoredrei,

    When you don't want the compiler changes the read-write order for optimization.

    And I have changed a little in above post.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 30, 2012 7:04 AM
  • >>The method VolatileWrite cannot sync the two different threads, it is designed for processor.

    So when should I use Volatile?

    Hi Ignoredrei,

    When you don't want the compiler changes the read-write order for optimization.

    And I have changed a little in above post.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Example,plz……
    Tuesday, July 31, 2012 9:23 AM
  • Hi ,

    Here is the example: http://sdrv.ms/N9UAtC 

    You can compare the result by comments Volatile keywords or not.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 1, 2012 5:52 AM