#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.
Definition at line 675 of file QThread.h.
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) |
~lockedAccessor () | |
type & | operator * () const |
type * | operator-> () const |