Multithreading: do I need protect my variable in read-only method?

前端 未结 5 1241
庸人自扰
庸人自扰 2021-01-12 09:34

I have few questions about using lock to protect my shared data structure. I am using C/C++/ObjC/Objc++

For example I have a counter class that used in multi-thread

5条回答
  •  半阙折子戏
    2021-01-12 10:22

    Yes, you would need to lock the read as well in this case.

    There are several alternatives -- a lock is quite heavy here. Atomic operations are the most obvious (lock-free). There are also other approaches to locking in this design -- the read write lock is one example.

提交回复
热议问题