Is this rule about volatile usage strict?

前端 未结 8 1770
梦毁少年i
梦毁少年i 2020-12-11 20:36

I\'ve seen this sentence:

the general rule is, if you have variables of primitive type that must be shared among multiple threads, declare those

8条回答
  •  隐瞒了意图╮
    2020-12-11 21:12

    I would propose a considerably more strict but very useful rule: if you do not understand exactly what volatile does, do not use it. Instead, use lock. If you do not understand exactly what lock does and how to use it, do not use multithreading.

提交回复
热议问题