Results 1 to 2 of 2

Thread: Make a QSharedPointer like class thread-safe with signal/slots

  1. #1
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Make a QSharedPointer like class thread-safe with signal/slots

    Hi,

    I create a class named SyncronizedPointer, it look like a QSharedPointer except that "->" operator make that all call become thread Safe here a simple example of use.

    Qt Code:
    1. class Foo
    2. {
    3. void criticalMethod()
    4. {
    5. //All work done here is completely thred-safe
    6. }
    7. };
    8.  
    9. int main()
    10. {
    11.  
    12. SyncronizedPointer<Foo> sp( new Foo() );
    13.  
    14. sp->criticalMethod();
    15. /* This precedent line will automatically:
    16.   - Lock an mutex inside SyncronizedPointer object.
    17.   - Call the criticalMethod
    18.   - Unlock the mutex
    19.  
    20.   With this system it's easy to make the Foo class thread safe, I can use copy of the
    21.   SyncronizedPointer object in multiple thread, I am sure that no more than one thread can
    22.   call a method inside Foo object at the same time.
    23.  */
    24.  
    25. return (0);
    26. /*
    27.   The Foo object will be automatically delete using a reference counting inside SyncronizedPointer.
    28.   */
    29. }
    To copy to clipboard, switch view to plain text mode 


    So my problem is when I am trying to use SyncronizedPointer with QObject like

    Qt Code:
    1. class Foo : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. void criticalMethod();
    8.  
    9. public slot:
    10.  
    11. void criticalSlot();
    12. }
    13.  
    14. int main()
    15. {
    16. SyncronizedPointer<Foo> sp(new Foo());
    17.  
    18. /*
    19.   Here no problem I can send the sp var across thread, call to criticalMethod will stay
    20.   thread safe.
    21.   */
    22.  
    23. QTimer timer;
    24.  
    25. /*
    26.   Now I want to connect the timer timeout signal to the criticalSlot
    27.   Note that call to this slot MUST be totaly thread safe
    28.   */
    29. QObject::connect(&timer, SIGNAL(timeout()), sp->data(), SLOT(criticalSlot()));
    30.  
    31. /*
    32.   The precedent line does not protect the access to the Foo class because Qt signal system
    33.   will directly call the slot without locking the Mutex inside the SyncronizedPointer
    34.  */
    35.  
    36. return (0);
    37. }
    To copy to clipboard, switch view to plain text mode 


    Is there a solution to make this work, for example by making Qt call the slot using the
    SyncronizedPointer object ?


    An easy way would be to put the mutex inside the Foo class and lock it inside both slot and method
    but the code would be much less sexy


    PS: Sorry if my English is not perfect.
    Last edited by yayo56; 9th August 2011 at 22:15.

  2. #2
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Make a QSharedPointer like class thread-safe with signal/slots

    If I understand your problem you want your pointer to act like a decorator for your instance of QObject. No matter what's called on the object you want the code to go through the operator overloading of the pointer first to synchronize the call and then delegate the execution to the object.

    Let me be honest, there's no such a thing in C++ (I'm talking about decorators).

    But it doesn't mean it's not possible with Qt. The problem is that you connect the signal to your object, so the compiler has no idea about the SynchronizedPointer that's taking care of it.
    What should be done is connecting the signal to the SynchronizedPointer instead, then delegate the execution to the encapsulated object's slot when the pointer receives the signal. You probably need to look deeper into Qt's meta objects and reflection documentation to come out with a generic solution but if that's not possible then you would need to write one pointer class for each type you want to decorate (not very exciting).

    PS : If the sexy solution works try writing a generic Decorator class, that would be awesome.
    Last edited by soft0613; 10th August 2011 at 16:50.

Similar Threads

  1. Are signals and slots thread safe?
    By Cruz in forum Qt Programming
    Replies: 12
    Last Post: 21st April 2011, 14:57
  2. Replies: 0
    Last Post: 22nd February 2011, 07:55
  3. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  4. Replies: 3
    Last Post: 19th January 2010, 20:26
  5. creating treeWidgetItem using emitted signal from thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 08:37

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.