Results 1 to 4 of 4

Thread: Call method of Object in different Thread

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Call method of Object in different Thread

    Hi, a little Question...

    I have several Objects, and each of this objects live in its own thread. (moveToThread)

    Further I have a pool, which has a QList<Object*>, which holds the objects. The Pool lives in the mainthread.

    Is it right, that if I call a method of an object within my pool-class, the method is processed in the object's thread? And Is it right, that I have to lock the member method with QMutex?

    thank u

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Call method of Object in different Thread

    Is it right, that if I call a method of an object within my pool-class, the method is processed in the object's thread?
    No. the method is processed(executed) in the calling thread (main thread) context.

    And Is it right, that I have to lock the member method with QMutex?
    You can use QMutex (for non-QObject based objects), but it will not work for any QObject based object's member functions.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    Qiieha (26th July 2012)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call method of Object in different Thread

    No, a method (more generally any function) is always executed in the same thread that called it. This has nothing to do with them being methods of QObjects who happen to have a notion of thread affinity.

    However, Qt contains some features allowing a programmer to easily trigger the execution of a method of a QObject in the thread controlling this QObject (which may be distinct from the current thread) provided this thread runs an event loop. One such way is to use signal-slot connections. Another way (which may be simpler in your case) is to use QMetaObject::invokeMethod().

    In any case I strongly recommend you read the docs on signals/slots across threads and QObject's thread affinity.

    I do not understand what you mean by "lock the member method with QMutex". All I can say is that QMutex is useful when you need to serialize access to a resource shared among sevaral threads.

  5. The following user says thank you to yeye_olive for this useful post:

    Qiieha (26th July 2012)

  6. #4
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call method of Object in different Thread

    Thank u for your replies!
    Now the issue is clear for me.

Similar Threads

  1. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 15:49
  2. C++ method call
    By ^NyAw^ in forum General Programming
    Replies: 4
    Last Post: 19th November 2011, 12:19
  3. None-GUI thread call widget method cause error
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2010, 05:39
  4. Replies: 6
    Last Post: 15th April 2010, 20:02
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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.