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

前端 未结 5 1202
庸人自扰
庸人自扰 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:21

    You cant gaurantee that multiple threads wont modify your variable at the same time. and if such a situation occurs your variable will be garbled or program might crash. In order to avoid such cases its always better and safer to make the program thread safe.

    You can use the synchronization techinques available like: Mutex, Lock, Synchronization attribute(available for MS c++)

提交回复
热议问题