Results 1 to 9 of 9

Thread: Ways to share data between threads

  1. #1
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Ways to share data between threads

    Hi, I need to share some variables between several treads and I'm not really sure about how to do it.
    Right now i think i can create a class to store the values and write the setters and getters with the lock mechanisms to protect the variables. Then i can pass a reference to the container object to all the involved threads.
    Would this be an effective way to share?
    The storage class would be like this:
    -header:
    Qt Code:
    1. #ifndef DATATABLE_H
    2. #define DATATABLE_H
    3.  
    4. #include <QObject>
    5. #include <QReadWriteLock>
    6.  
    7. class DataTable : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit DataTable(QObject *parent = 0);
    12. void setIntegerData(int);
    13. int getIntegerData();
    14.  
    15. private:
    16. int integerdata;
    17. };
    18.  
    19. #endif // DATATABLE_H
    To copy to clipboard, switch view to plain text mode 

    -implementation:
    Qt Code:
    1. #include "datatable.h"
    2.  
    3.  
    4. DataTable::DataTable(QObject *parent) :
    5. QObject(parent)
    6. {
    7. }
    8.  
    9. void DataTable::setIntegerData(int i)
    10. {
    11. QWriteLocker wlocker(&lock);
    12. integerdata=i;
    13. }
    14.  
    15. int DataTable::getIntegerData()
    16. {
    17. QReadLocker rlocker(&lock);
    18. return integerdata;
    19. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Ways to share data between threads

    If the class inherits QObject then that's not enough. But so far your class doesn't use its QObject legacy for anything so you can just remove the inheritance.
    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
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Ways to share data between threads

    Use QSharedMemory class

    Don't make bicycle
    Last edited by unit; 11th March 2011 at 13:13.
    Try read Qt documentation before ask stupid question.

  4. #4
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ways to share data between threads

    Thanks. I do not see anything that forces me to inherit QObject so that will go away. So after fixing that, can I consider this class to be thread safe or at least safe enough for my purposes?
    Just to learn more, why would this protection be insufficient? Right now I'm using in another part of the code a QQueue which is a member of a QThread. These two do inherit from QObject, so is my app in danger?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Ways to share data between threads

    Quote Originally Posted by AndresBarbaRoja View Post
    Thanks. I do not see anything that forces me to inherit QObject so that will go away. So after fixing that, can I consider this class to be thread safe or at least safe enough for my purposes?
    Yes.

    Just to learn more, why would this protection be insufficient?
    Because QObject is not thread-safe and since you don't have access to internals of QObejct, you can't protect your class from concurrent access. As long as you don't any QObject API this doesn't matter but then it doesn't make sense to inherit QObject in the first place.

    Right now I'm using in another part of the code a QQueue which is a member of a QThread. These two do inherit from QObject, so is my app in danger?
    QQueue doesn't inherit QObject. But it's also not thread-safe.
    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.


  6. #6
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ways to share data between threads

    Quote Originally Posted by wysota View Post
    QQueue doesn't inherit QObject. But it's also not thread-safe.
    Well, I block access to the queue before actually doing anythong to it, so i guess is the same as if it were an int
    Thanks for the replies
    (to both of you)


    Added after 22 minutes:


    Quote Originally Posted by unit View Post
    Use QSharedMemory class

    Don't make bicycle
    I'm reading the QSharedMemory docs but it appears to me that I lock the whole memory and not just parts. If it does not allow me to lock only one variable at a time i'll have to create a shared memory for each (or at least for groups of variables).
    I believe I currently need to define serval locks to provide protected access to different variables at the same time. Do you know if defining several shared memory instances would be worthy?
    And what does "bicycle" means? I am not a programmer (yet) so i lack a lot of concepts :S
    Last edited by AndresBarbaRoja; 11th March 2011 at 14:03.

  7. #7
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ways to share data between threads

    More changes!
    Since I need to protect each variable i've defined a template class to create a single protected variable:
    Qt Code:
    1. #ifndef PROTECTEDVARIABLE_H
    2. #define PROTECTEDVARIABLE_H
    3.  
    4. #include <QReadWriteLock>
    5.  
    6. template <class T> class ProtectedVariable
    7. {
    8. public:
    9. ProtectedVariable(T=0);
    10. void set(T);
    11. T get();
    12.  
    13. private:
    14. T value;
    15. };
    16.  
    17. template <class T> ProtectedVariable<T>::ProtectedVariable(T data)
    18. {
    19. set(data);
    20. }
    21.  
    22. template <class T> void ProtectedVariable<T>::set(T value)
    23. {
    24. QWriteLocker wlocker(&lock);
    25. this->value=value;
    26. }
    27.  
    28. template <class T> T ProtectedVariable<T>::get()
    29. {
    30. QReadLocker rlocker(&lock);
    31. return value;
    32. }
    33. #endif // PROTECTEDVARIABLE_H
    To copy to clipboard, switch view to plain text mode 
    Then in the data container I create the different variables.
    Qt Code:
    1. #include <protectedvariable.h>
    2.  
    3. class DataTable
    4. {
    5. public:
    6. ProtectedVariable<int> data;
    7. ProtectedVariable<double> differentData;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Now the question, when I access the variables as
    Qt Code:
    1. DataTable dt;
    2. dt.data.set(5);
    To copy to clipboard, switch view to plain text mode 
    Is my variable still really protected?
    Last edited by AndresBarbaRoja; 11th March 2011 at 16:27.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Ways to share data between threads

    Change your get methods to return a const reference instead of a copy if possible.

    Honestly I'm not sure your approach is a good idea. It's usually better to have a single mutex that guards access to a group of variables used together instead of having several mutexes to protect each variable separately. It might happen that you'll end up with a situation where two threads are walking the same code path and accessing the same protected variables in sequence and the "second" thread stops at every possible mutex along the way. Locking mutexes is quite expensive in terms of cpu usage.

    As for integers it's better to use QAtomicInt instead of protecting an integer variable with a mutex.
    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.


  9. #9
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ways to share data between threads

    Thanks for the insight, I have a lot to learn.
    In fact I think some variables can be grouped. I have to think how it can be done though.
    Maybe I can create several data storage objects... anyway, it's Friday afternoon! time to disconnect at least for a couple of hours

Similar Threads

  1. [SOLVED] share data between caller and slot
    By whites11 in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2010, 13:34
  2. How do I access data between threads
    By yodasoda in forum Qt Programming
    Replies: 3
    Last Post: 26th February 2010, 19:10
  3. What is the best way to share data between 2 programs?
    By ModeZt in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 21:23
  4. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 18:29
  5. Help me to use QtSharedMemory to share the data objects
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 11:50

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.