Volatile Violates its main job?

前端 未结 3 1171
栀梦
栀梦 2020-12-14 17:09

According to MSDN:

The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields tha

3条回答
  •  时光取名叫无心
    2020-12-14 17:34

    Well you are right. It is more elaborated in Joseph Albahari threading book/article.

    The MSDN documentation states that use of the volatile keyword ensures that the most up-to-date value is present in the field at all times. This is incorrect, since as we’ve seen, a write followed by a read can be reordered.

    http://www.albahari.com/threading/part4.aspx#_The_volatile_keyword

    Should I ( as a programmer ) need to prevent using this keyword-because of such weird behavior?

    It should be used only after knowing this weired behavior. It should not be used as a Magic keyword to retrieve latest values all the time in multithreaded environment.

    IMO, usage of volatile keyword should be avoided as probable bugs are hard to find out.

提交回复
热议问题