none
What is the difference between global and volatile variable respect to the assembly code generation RRS feed

  • Question

  • I know the difference between global and local variable access. Volatile variable can be modified outside the program.

    But assume that i am using global regular variable (int) between two functions. So the compiler has to generate the load and store instructions for every access of this variable. (Assuming no optimization as well as my code effectively using the variable.)

    The same is true for volatile variable also. So according to assembly code generation there is no difference between global and volatile variable. Am I correct? 

    Saturday, May 30, 2015 9:57 PM

Answers

  • Volatile implies that something external to a given function can access the variable, such as another thread or a device.  For example, while the compiler with a good optimizer might load a global variable into a register at the start of a function, and not store it back till the end of the function, if the variable is volatile the compiler will not do this.

    Volatile does not imply atomic actions (such as the InterlockedXXX functions), but it does indicate that the compiler should load the variable when it is being referenced.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by Boomi.s Sunday, May 31, 2015 6:52 AM
    Saturday, May 30, 2015 10:06 PM
  • it will see 1, never zero because you only have one thread of execution. if thread1func() and thread2fun() were executing simultaneously thread2fun() could see a zero value and the interlocked instruction bumps it to one. 

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Boomi.s Tuesday, December 1, 2015 7:03 PM
    Tuesday, December 1, 2015 7:46 AM
  • the way it is currently written, yes. only one core executes a thread a time. a thread may move to another core (by the scheduler), but it is transparent to the application and is coherent

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Boomi.s Tuesday, December 1, 2015 7:03 PM
    Tuesday, December 1, 2015 4:59 PM

All replies

  • Volatile implies that something external to a given function can access the variable, such as another thread or a device.  For example, while the compiler with a good optimizer might load a global variable into a register at the start of a function, and not store it back till the end of the function, if the variable is volatile the compiler will not do this.

    Volatile does not imply atomic actions (such as the InterlockedXXX functions), but it does indicate that the compiler should load the variable when it is being referenced.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by Boomi.s Sunday, May 31, 2015 6:52 AM
    Saturday, May 30, 2015 10:06 PM
  • InterlockedXXX - Will be atomic? Does it happen in one clock cycle?

    During this operation, the thread will not pre-empted even if the quantum expires or high priority thread ready to run or interrupt occurs by any device?

    Tuesday, December 1, 2015 12:55 AM
  • The interlocked operations resolve to a single instruction that is not interruptable.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Tuesday, December 1, 2015 1:08 AM
  • Thanks Don. I need one more clarification, i am asking this question only to understand the behavior even though it is bad programming practice.

    int Var  = 0;

    // Running on second core only once in the program life time, it runs before the Thread2Fun
    Thread1Fun() 
    {  
        Var = 1 // No locking
    }

    // Running on first core
    Thread2Fun()
    {
        InterlockedIncrement(Var); // Locking
    }

    main()
    {
    Thread1Fun(); 
    Thread2Fun();
    Printf(Var);
    }

    Questions?

    When the Thread2Fun() runs it sees the global variable value as 1, only because "InterlockedIncrement" it ensures the cache coherecny?

    If I am not using the "InterlockedIncrement", there is possibility that the Thread2Fun() will see the global variable value 0?



    Tuesday, December 1, 2015 6:20 AM
  • it will see 1, never zero because you only have one thread of execution. if thread1func() and thread2fun() were executing simultaneously thread2fun() could see a zero value and the interlocked instruction bumps it to one. 

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Boomi.s Tuesday, December 1, 2015 7:03 PM
    Tuesday, December 1, 2015 7:46 AM
  • it will see 1, >> even though thread1func() executing on second core (and data cached), thread2fun() executing on first core.. Second core modification to the variable  will be visible to first core?

    Tuesday, December 1, 2015 4:08 PM
  • the way it is currently written, yes. only one core executes a thread a time. a thread may move to another core (by the scheduler), but it is transparent to the application and is coherent

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Boomi.s Tuesday, December 1, 2015 7:03 PM
    Tuesday, December 1, 2015 4:59 PM