What is the “volatile” keyword used for?

前端 未结 8 941
一个人的身影
一个人的身影 2020-11-28 19:35

I read some articles about the volatile keyword but I could not figure out its correct usage. Could you please tell me what it should be used for in C# and in

8条回答
  •  失恋的感觉
    2020-11-28 20:22

    To understand what volatile does to a variable, it's important to understand what happens when the variable is not volatile.

    • Variable is Non-volatile

    When two threads A & B are accessing a non-volatile variable, each thread will maintain a local copy of the variable in it's local cache. Any changes done by thread A in it's local cache won't be visible to the thread B.

    • Variable is volatile

    When variables are declared volatile it essentially means that threads should not cache such a variable or in other words threads should not trust the values of these variables unless they are directly read from the main memory.

    So, when to make a variable volatile?

    When you have a variable which can be accessed by many threads and you want every thread to get the latest updated value of that variable even if the value is updated by any other thread/process/outside of the program.

提交回复
热议问题