Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Returning QReadLocker by value from a function?
Forum Updated to NodeBB v4.3 + New Features

Returning QReadLocker by value from a function?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qreadlocker
2 Posts 2 Posters 255 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    Hi, I have a class where I use a QReadWriteLock to protect access to its data while reading and writing. Since the class has to emit signals when its data changes, I have made the QReadWriteLock private and only provided read-locking (lockForRead(), tryLockForRead(int timeout), and unlock()) access in its public interface. I would also like to provide a QReadLocker access to this lock, I am wondering if this is the correct way to provide that access:

    .h file

    class MyData : public QObject
    {
        Q_OBJECT
    public:
        QReadLocker getLocker();
    private:
        QReadWriteLock lock;
    }
    

    .cpp file

    QReadLocker MyData::getLocker() { return QReadLocker{&lock}; }
    

    I have tested it in a simple class and it seems to work fine. But I am not 100% sure if this would hold in real-world usage because of the confusion about copy and move constructors when returning an object by value from a function in C++. Are there any downsides to providing QReadLocker object as a return value from a function?

    Kent-DorfmanK 1 Reply Last reply
    0
    • CJhaC CJha

      Hi, I have a class where I use a QReadWriteLock to protect access to its data while reading and writing. Since the class has to emit signals when its data changes, I have made the QReadWriteLock private and only provided read-locking (lockForRead(), tryLockForRead(int timeout), and unlock()) access in its public interface. I would also like to provide a QReadLocker access to this lock, I am wondering if this is the correct way to provide that access:

      .h file

      class MyData : public QObject
      {
          Q_OBJECT
      public:
          QReadLocker getLocker();
      private:
          QReadWriteLock lock;
      }
      

      .cpp file

      QReadLocker MyData::getLocker() { return QReadLocker{&lock}; }
      

      I have tested it in a simple class and it seems to work fine. But I am not 100% sure if this would hold in real-world usage because of the confusion about copy and move constructors when returning an object by value from a function in C++. Are there any downsides to providing QReadLocker object as a return value from a function?

      Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      @CJha

      @CJha said in Returning QReadLocker by value from a function?:

      Are there any downsides to providing QReadLocker object as a return value from a function?

      Yes. It won't work. semaphores are special kinds of objects. By design, they should never be copied.

      Take simple counting semaphore. the count indicates whether a resource can acquire it. They are usually nothing more than an integer that is guaranteed atomic access. when you copy the integer the references are still to the old object, not the new one.

      On a larger note, I think your whole model is kind of flawed. Don't "wrap" semaphores. Therein lies the introduction of race conditions. Use a reference to the sole instance of a semaphore anywhere it is needed.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved