Results 1 to 2 of 2

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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