Volatile and CreateThread

前端 未结 6 452
借酒劲吻你
借酒劲吻你 2020-12-06 07:52

I just asked a question involving volatile: volatile array c++

However my question spawned a discussion on what volatile does.

Some claim that w

6条回答
  •  [愿得一人]
    2020-12-06 08:52

    Can you ALWAYS be sure that thread 1 will stop if done is not volatile?

    Always? No. But in this case the assignment to done is in the same module, and the while loop will probably not be optimized out. Depends on how the MSVC performs its optimizations.

    Generally, it is safer to declare it with volatile to avoid uncertainty with optimizations.

提交回复
热议问题