Results 1 to 13 of 13

Thread: QThread communicate threadsafe

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: QThread communicate threadsafe

    If someMethod() is a slot you can do this:

    Qt Code:
    1. QMetaObejct::invokeMethod(&obj[10], "someMethod", Qt::QueuedConnection);
    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.


  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread communicate threadsafe

    Quote Originally Posted by Ini View Post
    this is not threadsafe
    You mean because obj[10] could have been deleted by its thread?

    Because that is the only inherently unsafe part in that call.

    Quote Originally Posted by wysota View Post
    If someMethod() is a slot you can do this:

    Qt Code:
    1. QMetaObejct::invokeMethod(&obj[10], "someMethod", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    That of course also accesses "obj" without any guard, so it has the same problem as the direct method call.
    The only difference is that "someMethod" will be executed by the object's owner thread, which also has to run an event loop to be able to do that.

    Cheers,
    _
    Last edited by anda_skoa; 3rd June 2016 at 08:46.

  3. #3
    Join Date
    Nov 2015
    Posts
    67

    Default Re: QThread communicate threadsafe

    Quote Originally Posted by anda_skoa View Post
    You mean because obj[10] could have been deleted by its thread?

    Because that is the only inherently unsafe part in that call.



    That of course also accesses "obj" without any guard, so it has the same problem as the direct method call.
    The only difference is that "someMethod" will be executed by the object's owner thread, which also has to run an event loop to be able to do that.

    Cheers,
    _
    thx for explaing. First I want to say that I dont't want to use invokeMethod cause there is not compile-time errors if i write the functionname wrong. Just want to point that out, you must not comment that

    Then i have two questions. Why always people say use Signals&Slots when it seems there is like no advantage in that, when the only possilbility that the call is unsafe is that the obj gets deleted? Is it cause you have then Connectiontypes available?

    This leads my to my second question. What connectiontype would be equal to obj[10].someMethod(); ?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread communicate threadsafe

    Quote Originally Posted by Ini View Post
    thx for explaing. First I want to say that I dont't want to use invokeMethod cause there is not compile-time errors if i write the functionname wrong. Just want to point that out, you must not comment that
    Well, you don't have that with the SIGNAL/SLOT macros either.

    Quote Originally Posted by Ini View Post
    Why always people say use Signals&Slots when it seems there is like no advantage in that, when the only possilbility that the call is unsafe is that the obj gets deleted?
    Using signal/slot or invokeMethod makes it easy to cross thread boundaries, make the target thread execute a method.
    Especially nice when communicating from a worker thread to the main thread.

    Quote Originally Posted by Ini View Post
    What connectiontype would be equal to obj[10].someMethod(); ?
    Qt::DirectConnection

    Cheers,
    _

  5. #5
    Join Date
    Nov 2015
    Posts
    67

    Default Re: QThread communicate threadsafe

    "Well, you don't have that with the SIGNAL/SLOT macros either."

    I will get an compile error if the Slot does not exist that i type in with connect. With invokemethod i do not get an compile-error.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread communicate threadsafe

    Quote Originally Posted by Ini View Post
    I will get an compile error if the Slot does not exist that i type in with connect.
    No, not with the SIGNAL and SLOT macros.

    That type of connect does runtime checking, not compile time checking.
    SIGNAL and SLOT convert their argument into a string, very similar to what you do manually when using invokeMethod.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. Replies: 5
    Last Post: 24th February 2012, 06:49
  3. Replies: 3
    Last Post: 20th September 2011, 20:13
  4. How to communicate between two forms
    By iamjayanth in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2009, 08:17
  5. How to communicate between two different exe
    By Lele in forum Qt Programming
    Replies: 4
    Last Post: 9th July 2006, 13:02

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.