Results 1 to 13 of 13

Thread: QThread communicate threadsafe

  1. #1
    Join Date
    Nov 2015
    Posts
    67

    Default QThread communicate threadsafe

    I know it's not threadsafe to call a function of another thread in Mainthread. Is it allowed to call a signal form another thread without any problems? Lets say this code is in MainWindow:
    Qt Code:
    1. MainWindow.cpp
    2. thread->moveTOThread(obj);
    3. emit obj->Signal();
    To copy to clipboard, switch view to plain text mode 

    or is this the way to go:
    Qt Code:
    1. Obj.h
    2. signals:
    3. void testSignal();
    4. public slots:
    5. void Func();
    6.  
    7. MainWindow.h
    8. void testSignal();
    9.  
    10. MainWindow.cpp
    11. thread->moveTOThread(obj);
    12. connect(this,SIGNAL(testSignal()),obj,SIGNAL(testSignal()));
    13. emit testSignal();
    14.  
    15. Obj.cpp
    16. connect(this,SIGNAL(testSignal()),this,SLOT(Func()));
    To copy to clipboard, switch view to plain text mode 
    thank you
    Last edited by Ini; 1st June 2016 at 22:56.

  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
    I know it's not threadsafe to call a function of another thread in Mainthread. Is it allowed to call a signal form another thread without any problems? Lets say this code is in MainWindow:
    Qt Code:
    1. MainWindow.cpp
    2. thread->moveTOThread(obj);
    3. emit obj->Signal();
    To copy to clipboard, switch view to plain text mode 
    Sending another object's signal is generally a bad idea, better consider signals to have the same access restrictions as protected methods.

    In any case, this just emits the signal in the context of the main thread.

    Quote Originally Posted by Ini View Post
    or is this the way to go:
    Qt Code:
    1. Obj.h
    2. signals:
    3. void testSignal();
    4. public slots:
    5. void Func();
    6.  
    7. MainWindow.h
    8. void testSignal();
    9.  
    10. MainWindow.cpp
    11. thread->moveTOThread(obj);
    12. connect(this,SIGNAL(testSignal()),obj,SIGNAL(testSignal()));
    13. emit testSignal();
    14.  
    15. Obj.cpp
    16. connect(this,SIGNAL(testSignal()),this,SLOT(Func()));
    To copy to clipboard, switch view to plain text mode 
    Why not just connect to the slot directly instead of first connecting to a signal and then connecting that signal to a slot?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    67

    Default Re: QThread communicate threadsafe

    obj is an array in my code i just simplified it here. Then i would need an array of signals right? When I only want to adress one part of the array eg obj[2], when I emit the signal.

    If that works with the array of signals i had a thinking error.

  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

    There is no such thing as an array of signals.

    Maybe you could describe what you are currently trying to solve?

    Cheers,
    _

  5. #5
    Join Date
    Nov 2015
    Posts
    67

    Default Re: QThread communicate threadsafe

    mainwindow.cpp

    Obj obj[maxObjectCount];
    Obj thread[maxObjectCount];
    for (int i = 0; i < maxObjectCount; i++) {
    obj[i].moveToThread(thread[i])
    }

    Now i need to call a Function of lets say obj[10] in mainwindow.cpp

    Idk know at all how to do that. Nobody can exlain threads that sb understand it seems, maybe nobody understands them I'm thinking ^^

  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

    So you need to call a method in an object and you are asking how to emit a signal?

    Qt Code:
    1. obj[10].someMethod();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Nov 2015
    Posts
    67

    Default Re: QThread communicate threadsafe

    this is not threadsafe

  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: 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.


  9. #9
    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.

  10. #10
    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(); ?

  11. #11
    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,
    _

  12. #12
    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.

  13. #13
    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.