0

thread safe and const member function

Hi I read somewhere that const member functions are always thread safe as it is read only operation on member variables. But what if other function is modifying the data which we are reading?

24th Jan 2025, 12:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Answers
+ 1
Ketan Lalcheta the const memeber functions are nothing but the constant values which cannot be changed after defining it. So if you don't want the variable values to get changed you can use const memeber function for achieving it as any other functions can't change the values of it.
24th Jan 2025, 3:30 PM
Aysha
0
atomic? volatile have conflicting pros and cons... https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading Feeding changing data to threads is tricky and error prone...
24th Jan 2025, 4:28 PM
Bob_Li
Bob_Li - avatar
0
Hi Bob_Li, Atomic is very low level and need more care. I would go with mutex then but point is that const member function is not automatically thread safe and need some mechanism be it atomic or mutex. Right?
24th Jan 2025, 4:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
HI Aysha Could not agree more...! Const member function can have access of any member variables. It just a guarantee to compiler that this const function will not modify anything.
24th Jan 2025, 4:39 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
but fo you have guarantee that the value is up to date?
24th Jan 2025, 5:21 PM
Bob_Li
Bob_Li - avatar