История изменений
Исправление wandrien, (текущая версия) :
const MyLockHolder badLock = other.get_lock();
class MyClass {
public:
...
private:
class LockedContext {
public:
LockedContext(MyClass *_that) : that(_that) { /* acquire lock */ }
~LockedContext() { /* release lock */ }
void do_something() {
that->...
}
private:
LockedContext(const LockedContext&) = delete;
LockedContext(LockedContext&&) = delete;
LockedContext& operator = (const LockedContext&) = delete;
LockedContext& operator = (LockedContext&&) = delete;
LockedContext* operator &() = delete;
MyClass *that;
};
void foo() {
LockedContext lck(this);
lck.do_something();
}
};
Исправление wandrien, :
const MyLockHolder badLock = other.get_lock();
class MyClass {
public:
...
private:
class LockedContext {
public:
LockedContext(MyClass *_that) : that(_that) { /* acquire lock */ } ~LockedContext() { /* release lock */ }
void do_something() {
that->...
}
private:
LockedContext(const LockedContext&) = delete;
LockedContext(LockedContext&&) = delete;
LockedContext& operator = (const LockedContext&) = delete;
LockedContext& operator = (LockedContext&&) = delete;
LockedContext* operator &() = delete;
MyClass *that;
};
void foo() {
LockedContext lck(this);
lck.do_something();
}
};
Исправление wandrien, :
const MyLockHolder badLock = other.get_lock();
class MyClass {
public:
...
private:
class LockedContext {
public:
static LockedContext lock(MyClass *that) { return LockedContext(that); }
~LockedContext() { /* release lock */ }
void do_something() {
that->...
}
private:
LockedContext(const LockedContext&) = delete;
LockedContext(LockedContext&&) = delete;
LockedContext& operator = (const LockedContext&) = delete;
LockedContext& operator = (LockedContext&&) = delete;
LockedContext* operator &() = delete;
LockedContext(MyClass *_that) : that(_that) { /* acquire lock */ }
MyClass *that;
};
void foo() {
auto lck = LockedContext::lock(this);
lck.do_something();
}
};
Исходная версия wandrien, :
const MyLockHolder badLock = other.get_lock();
class MyClass {
public:
...
private:
class LockedContext {
public:
static LockedContext lock(MyClass *that) { return LockedContext(that); }
~LockedContext() { /* release lock */ }
void do_something() {
...
}
private:
LockedContext(const LockedContext&) = delete;
LockedContext(LockedContext&&) = delete;
LockedContext& operator = (const LockedContext&) = delete;
LockedContext& operator = (LockedContext&&) = delete;
LockedContext* operator &() = delete;
LockedContext(MyClass *_that) : that(_that) { /* acquire lock */ }
MyClass *that;
};
void foo() {
auto lck = LockedContext::lock(this);
lck.do_something();
}
};