Results 1 to 9 of 9

Thread: Ways to share data between threads

Hybrid View

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

    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.

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

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    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
  •  
Qt is a trademark of The Qt Company.