130. RAII

Question.2

A developer protects shared data with a scoped mutex:

class MutexGuard {
   Mutex& mtx;
public:
   MutexGuard(Mutex& m) : mtx(m) { mtx.lock(); }
   ~MutexGuard() { mtx.unlock(); }
   MutexGuard(const MutexGuard&) = delete;
   MutexGuard& operator=(const MutexGuard&) = delete;
};

void update_sensor() {
   MutexGuard guard(data_mutex);
   sensor_data = read_adc();
}

Why is copying deleted for MutexGuard?

Need Help? Refer to the Quick Guide below

Select Answer