Are you sure of that??I'd like to make lockForRead/lockForWrite methods to be const, because they don't change any data
Probably they change the status of internal data!!
What do you need? Probably there is an other way to obtain that
Are you sure of that??I'd like to make lockForRead/lockForWrite methods to be const, because they don't change any data
Probably they change the status of internal data!!
What do you need? Probably there is an other way to obtain that
Last edited by mcosta; 19th July 2011 at 11:13. Reason: updated contents
A camel can go 14 days without drink,
I can't!!!
Perhaps, they change something in QReadWriteLock, but I use multiple inheritance when I want to protect some list for example. See example:
Qt Code:
{ //... overriding lockForRead and lockForWrite methods } class MyClass { public: const FileList & getFiles() const; void modifyFiles(); protected: FileList fileList; }To copy to clipboard, switch view to plain text mode
So I'd like to be able to read const FileList & (returned by getFiles) protecting it with lockForRead.
Write as
Qt Code:
class FileList: public QList<MyFile> { void readLock () const {m_lock.readForRead();} void writeLock () const {m_lock.readForWrite();} private: mutable QReadWriteLock m_lock; } class MyClass { public: const FileList & getFiles() const; void modifyFiles(); protected: FileList fileList; }To copy to clipboard, switch view to plain text mode
However IMO isn't correct to make the lock functions const. They modify the internal state of the object
A camel can go 14 days without drink,
I can't!!!
yes, but then I am unable to use it along with QReadLocker.Write as...
then how should i check my file list in another thread?However IMO isn't correct to make the lock functions const. They modify the internal state of the object
Qt Code:
bool findFile() { const FileList &fileList = myclass->getFiles(); // need to protect fileList here fileList.lockForRead(); // i don't modify file list here, but i need it to be protected from modification with something else foreach (const MyFile &file, fileList) { //... } fileList.unlock(); }To copy to clipboard, switch view to plain text mode
In a previous post I wrote
If you need to protect the access to a collection, you should write wrapper functions that completely hide the internal data structure.What do you need? Probably there is an other way to obtain that
Instead of using inheritance, you should think to use composition and implement the interface to manipulate internal data.
if you leave to the client code the responsibility to call *Lock functions, you have to call them explicitly each time you need.
A camel can go 14 days without drink,
I can't!!!
If there is, I don't know about that way.What do you need? Probably there is an other way to obtain that
not really possible, fileList is used many times in different functions that require some things that have no relation to MyClass object where as far as i understood i should add a wrapper function.If you need to protect the access to a collection, you should write wrapper functions that completely hide the internal data structure.
Multiple inheritance is more convenient and i can use it along with QReadLocker for example. I rather keep it like i have now doing this way: const_cast<FileList&>.lockForRead().Instead of using inheritance, you should think to use composition and implement the interface to manipulate internal data.
Well, i call it explicitly in findFile function (that is a client function). Or did you mean something else?if you leave to the client code the responsibility to call *Lock functions, you have to call them explicitly each time you need.
Bookmarks