#include <QThread.h>
As anyone familiar with multithreaded programming is aware, often one must make a choice between efficiency and thread-safety when writing member functions of a lockable class eg;
QStringList Class::foo() const { QMtxHold h(this); return p->mystrings(); }
lockedAccessor solves this problem by extending the hold of the lock past the return of the member function:
Generic::lockedAccessor<const QStringList> Class::foo() const { Generic::lockedAccessor<const QStringList> ret(p->mystrings(), this); // You can do stuff to ret as though it were p->mystrings() return ret; }
class->foo()
.empty(), but you still must be careful - if you write QStringList list(class->foo());
then you are safe, but if you use the return directly then you can lose the lock inadvertently. Public Member Functions | |
lockedAccessor (type &_val, const QMutex *m) | |
lockedAccessor (type &_val, const QMutex &m) | |
lockedAccessor (type &_val, const QRWMutex *m) | |
lockedAccessor (type &_val, const QRWMutex &m) | |
lockedAccessor (lockedAccessor &o) | |
lockedAccessor & | operator= (lockedAccessor &o) |
type & | operator* () const |
type * | operator-> () const |
FX::Generic::lockedAccessor< type >::lockedAccessor | ( | type & | _val, | |
const QMutex * | m | |||
) | [inline] |
type& FX::Generic::lockedAccessor< type >::operator* | ( | ) | const [inline] |
Returns the value.
type* FX::Generic::lockedAccessor< type >::operator-> | ( | ) | const [inline] |
Returns the value.