abalfazl hossein wrote:If flag is a volatile variable, Then why isn't used volatile keyword in code?
Because the author didn't do a good job of writing clearly. He's really talking about two different cases, but he's only showing the code once.
1. If "flag" is not declared as volatile, as shown, then the code marked "//this could happen" may execute, as explained in the article (even though that seems to make no logical sense to most people).
2. If "flag"
is declared as volatile, (which is
not shown here) then the code marked "//this could happen" can never happen. Volatile fixes the problem described in this section.