Results 1 to 7 of 7

Thread: Overriding QReadWriteLock's lockForRead and lockForWrite

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Overriding QReadWriteLock's lockForRead and lockForWrite

    I am interested if there is any way to change the functionality of QReadWriteLock's lockForRead and lockForWrite methods while retaining the ability of using them along with QReadLocker and QWriteLocker classes. I noticed lockForRead and lockForWrite methods of QReadWriteLock are not virtual, so when passing derived class to QReadLocker it calls old method. More specifically, I'd like to make lockForRead/lockForWrite methods to be const, because they don't change any data and I think it should be possible to call them on const data. Also I want to add some reporting, so it would be easier to know if some object is locked.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    I'd like to make lockForRead/lockForWrite methods to be const, because they don't change any data
    Are you sure of that??

    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!!!

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    Perhaps, they change something in QReadWriteLock, but I use multiple inheritance when I want to protect some list for example. See example:
    Qt Code:
    1. class FileList: public QList<MyFile>, public QReadWriteLock
    2. {
    3. //... overriding lockForRead and lockForWrite methods
    4. }
    5.  
    6. class MyClass
    7. {
    8. public:
    9. const FileList & getFiles() const;
    10. void modifyFiles();
    11. protected:
    12. FileList fileList;
    13. }
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    Write as

    Qt Code:
    1. class FileList: public QList<MyFile>
    2. {
    3. void readLock () const {m_lock.readForRead();}
    4. void writeLock () const {m_lock.readForWrite();}
    5.  
    6. private:
    7. mutable QReadWriteLock m_lock;
    8. }
    9.  
    10. class MyClass
    11. {
    12. public:
    13. const FileList & getFiles() const;
    14. void modifyFiles();
    15. protected:
    16. FileList fileList;
    17. }
    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!!!

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    Write as...
    yes, but then I am unable to use it along with QReadLocker.

    However IMO isn't correct to make the lock functions const. They modify the internal state of the object
    then how should i check my file list in another thread?
    Qt Code:
    1. bool findFile()
    2. {
    3. const FileList &fileList = myclass->getFiles();
    4.  
    5. // need to protect fileList here
    6. fileList.lockForRead();
    7.  
    8. // i don't modify file list here, but i need it to be protected from modification with something else
    9. foreach (const MyFile &file, fileList)
    10. {
    11. //...
    12. }
    13.  
    14. fileList.unlock();
    15. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    In a previous post I wrote

    What do you need? Probably there is an other way to obtain that
    If you need to protect the access to a collection, you should write wrapper functions that completely hide the internal data structure.
    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!!!

  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Overriding QReadWriteLock's lockForRead and lockForWrite

    What do you need? Probably there is an other way to obtain that
    If there is, I don't know about that way.

    If you need to protect the access to a collection, you should write wrapper functions that completely hide the internal data structure.
    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.

    Instead of using inheritance, you should think to use composition and implement the interface to manipulate internal data.
    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().

    if you leave to the client code the responsibility to call *Lock functions, you have to call them explicitly each time you need.
    Well, i call it explicitly in findFile function (that is a client function). Or did you mean something else?

Similar Threads

  1. QReadWriteLock Question
    By qtYoda in forum Newbie
    Replies: 9
    Last Post: 7th April 2011, 11:03
  2. Overriding global new
    By branko in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2010, 16:10
  3. overriding QListWidget advice
    By codebehind in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2010, 22:39
  4. using QReadWriteLock in QMap and Qhash
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 13:00
  5. Overriding drawRubberBand()
    By andrew.nguyen in forum Qwt
    Replies: 3
    Last Post: 21st April 2010, 06:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.