Do I need to use locking with integers in c++ threads

前端 未结 7 1472
慢半拍i
慢半拍i 2021-01-04 21:10

If I am accessing a single integer type (e.g. long, int, bool, etc...) in multiple threads, do I need to use a synchronisation mechanism such as a mutex to lock them. My un

7条回答
  •  天涯浪人
    2021-01-04 21:20

    Yes. If you are on Windows you can take a look at Interlocked functions/variables and if you are of the Boost persuasion then you can look at their implementation of atomic variables.

    If boost is too heavyweight putting "atomic c++" into your favourite search engine will give you plenty of food for thought.

提交回复
热议问题