Skip to content

Commit

Permalink
Make Lock in synchronized.h safer
Browse files Browse the repository at this point in the history
class instead of struct.
Private myObject.
Lock extends NoCopy.
Constructor is now explicit.
  • Loading branch information
dnikulin committed Jul 8, 2011
1 parent 933362a commit 5b64295
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cvd/synchronized.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,21 @@ class Synchronized : private NoCopy
A Lock object should be declared on the stack in the same scope as the object to be locked.
When the Lock object goes out of scope, the mutex is released. This is especially useful
in code that throws exceptions. */
struct Lock {
const Synchronized& myObject;
Lock(const Synchronized& obj) : myObject(obj) { myObject.lock(); }
virtual ~Lock() { myObject.unlock(); }
class Lock : private NoCopy {
public:

explicit Lock(const Synchronized& obj)
: myObject(obj) {
myObject.lock();
}

~Lock() {
myObject.unlock();
}

private:

const Synchronized& myObject;
};

}
Expand Down

0 comments on commit 5b64295

Please sign in to comment.