Волатильно Нарушает свою основную работу?
В соответствии сMSDN:
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
Пожалуйста, обратите внимание на последнее предложение:
This ensures that the most up-to-date value is present in the field at all times.
Однако существует проблема с этим ключевым словом.
Я & APOS; веread что это может изменить порядок инструкций:
First instruction Second instruction Can they be swapped?
Read Read No
Read Write No
Write Write No
Write Read Yes! <----
Это означает, что Джон устанавливает значение для изменчивого поля, иlater Пол хочет прочитать поле, Пол получаетold значение!
Что здесь происходит? Разве это не основная работа?
I know there are other solutions, but my question is about the volatile keyword.
Должен ли я (как программист) отказаться от использования этого ключевого слова - из-за такого странного поведения?