我有一个可以从多个线程访问的类.getter和setter函数都有锁.是否需要用于吸气功能的锁?为什么?
class foo {
public:
void setCount (int count) {
boost::lock_guard<boost::mutex> lg(mutex_);
count_ = count;
}
int count () {
boost::lock_guard<boost::mutex> lg(mutex_); // mutex needed?
return count_;
}
private:
boost::mutex mutex_;
int count_;
};
Run Code Online (Sandbox Code Playgroud)