I have some private variables (e.g. int a, int b, int c) inside my class. Due to some internal manipulation, I need to set / get such variables in thread safe mode, so I used some wrapper getters / setters and used a mutex with scope.
void setA(int a) { unique_lock<mutex> lock(opMutex); this->a = a; } void getA(int a) { unique_lock<mutex> lock(opMutex); return a; } void setB(int b) { unique_lock<mutex> lock(opMutex); this->b = b; } void setC(int c) { unique_lock<mutex> lock(opMutex); this->c = c; }
My question is: is it possible to avoid getter / setter methods (public variables) and maintain thread safety when performing input / read operations on such variables?
source share