Results 1 to 6 of 6

Thread: Using QVariant class variable in a thread safe manner.

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using QVariant class variable in a thread safe manner.

    I have one Reader and one Writer Thread. Reader reads from class member QVariant val and Writer writes into val. Now I want to do it in a synchronized manner. I implemented in this manner:
    itm.h
    Qt Code:
    1. #ifndef ITM_H
    2. #define ITM_H
    3.  
    4. #include <QObject>
    5. #include <QVariant>
    6. #include <QMutex>
    7. class Itm : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit Itm(QObject *parent = 0);
    12. QMutex mutex;
    13. void setValue(const QVariant& newVal);
    14. QVariant getValue() ;
    15. private:
    16. QVariant val;
    17.  
    18.  
    19. };
    20.  
    21. #endif // ITM_H
    22. }
    To copy to clipboard, switch view to plain text mode 
    and itm.cpp
    Qt Code:
    1. #include "itm.h"
    2. #include <QMutexLocker>
    3. Itm::Itm(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. void Itm::setValue(const QVariant &newVal)
    9. {
    10. QMutexLocker locker(&mutex);
    11. val = newVal;
    12. }
    13.  
    14. QVariant Itm::getValue()
    15. {
    16. QMutexLocker locker(&mutex);
    17. return val;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Writer thread uses setValue() function and the reader getValue() function. Now the qt documentation says here that "If a function is not marked as thread-safe or reentrant, it should not be used from different threads. If a class is not marked as thread-safe or reentrant then a specific instance of that class should not be accessed from different threads." Now QVariant has not been marked as reentrant in the documentation. So does that mean that the above method I am trying to implement to achieve synchronization is ill-advised, and if used across threads, QVariants is NOT an option. Or I am misinterpreting??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QVariant class variable in a thread safe manner.

    You are misinterpreting, your code is ok, although you can change it to:

    Qt Code:
    1. private:
    2.  
    3. void Itm::setValue(const QVariant &newVal)
    4. {
    5. QWriteLocker locker(&mutex);
    6. val = newVal;
    7. }
    8.  
    9. QVariant Itm::getValue()
    10. {
    11. QReadLocker locker(&mutex);
    12. return val;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QVariant class variable in a thread safe manner.

    Doesn't it mean that the statement 'If a function is not marked as thread-safe or reentrant, it should not be used from different threads. If a class is not marked as thread-safe or reentrant then a specific instance of that class should not be accessed from different threads.' should not have been present in the document fore-mentioned? Because essentially I use the operator overload = / or may be setValue of QVariant which is non-reentrant. Does having a different instance of QVariant for each of Itm classes, makes it by default re-entrant?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QVariant class variable in a thread safe manner.

    The statement actually means (at least in most cases) that since the class/method is not reentrant/thread-safe, you can't use with directly without proper synchronization. So whenever you call any non-const method (or actually a non non-mutable method) on the variant, you should protect it with a mutex. Since reading a half-written object is no fun either, you should be protecting the non-mutable methods too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QVariant class variable in a thread safe manner.

    Alright I get it now. One more question. Does this mean if I write a function which multiple threads can run concurrently, which is not re-entrant, can be made thread-safe by use of mutex?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QVariant class variable in a thread safe manner.

    Technically the answer to your question is "no", because you are incorrectly using the terms reentrant and thread-safe. A reentrant function doesn't have to be thread-safe (and vice-versa).

    http://en.wikipedia.org/wiki/Reentrant_(subroutine)
    http://en.wikipedia.org/wiki/Thread_safety

    Logically, yes, correctly using a mutex can turn a thread-unsafe method into a thread-safe one. Using a mutex can also cause a reentrant function to become not reentrant (using a non-recursive mutex may cause a deadlock when the function is called again by the same thread).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 10th August 2011, 16:46
  2. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  3. Why is QMutex Thread Safe?
    By Kind Lad in forum Newbie
    Replies: 3
    Last Post: 22nd February 2010, 04:46
  4. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16
  5. Link QVariant to global variable
    By jobrandt in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2007, 10:18

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.