Results 1 to 7 of 7

Thread: Unsure about QThreadPool object ownership

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Unsure about QThreadPool object ownership

    Qt help presents following code sample in QThreadPool section:

    HelloWorldTask *hello = new HelloWorldTask();
    // QThreadPool takes ownership and deletes 'hello' automatically
    QThreadPool::globalInstance()->start(hello);
    Does it mean that "moveToThread" is called behind the scene and "hello" instance is running in the content of the currently available thread in QThreadPool?
    If so, is it possible to verifiy it? I searched qthreadpool.cpp and did not find direct reference to "movetoThread" call.

    Thanks.
    Last edited by TorAn; 5th September 2016 at 21:46.

  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: Unsure about QThreadPool object ownership

    Quote Originally Posted by TorAn View Post
    Does it mean that "moveToThread" is called behind the scene
    No, a QRunnable is not a QObject.

    Quote Originally Posted by TorAn View Post
    "hello" instance is running in the content of the currently available thread in QThreadPool?
    Yes, the runnable's run() method is executed by the thread pool thread allocated for that task.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unsure about QThreadPool object ownership

    So, for example, if my class has member variables that are created in the constructor, all of them will be created in the context of the caller's thread, i.e the thread that creates the instance of my class. But the "run" method is executed in the context of the allocated thread pool thread? In the example below member variable _b is created in the context of the main thread, but variable _c is created in the context of thread pool thread?


    class B{};
    class A : public QRunnable
    {
    private:
    B* _b;
    B* _c;
    public:
    A() : _b(new B()) {}
    void run() {
    _c = new B();
    }
    }

    void main() {
    A* a = new A();
    QThreadPool::globalInstance()->start(a);
    }

  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: Unsure about QThreadPool object ownership

    Quote Originally Posted by TorAn View Post
    In the example below member variable _b is created in the context of the main thread, but variable _c is created in the context of thread pool thread?
    Yes.

    Any specific reason why this is important for you? Is B a QObject derived class?

    Cheers,
    _

  5. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unsure about QThreadPool object ownership

    Yes, a couple. I have this very construct in my code and it looks like the execution goes in main thread and I don't know how to verify if it is or not. Second of all, I want to fully understand how to properly arrange the code for usage with Qrunnable.

    In my specific case yes, "B" is the QObject-derived. But what if it is not?

  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: Unsure about QThreadPool object ownership

    Quote Originally Posted by TorAn View Post
    Yes, a couple. I have this very construct in my code and it looks like the execution goes in main thread and I don't know how to verify if it is or not.
    You can check QThread::currentThread() against QCoreApplication::instance()->thread()

    Quote Originally Posted by TorAn View Post
    In my specific case yes, "B" is the QObject-derived. But what if it is not?
    If it is not then it doesn't matter which thread created it as long as only one thread access it or access is protected against concurrent access.

    Instances of QObject derived classes are different because QObject has a thread affinity, events for such an object are handled by the event loop of the object's thread, QObject::connect() of type Qt::AutoConnection become queued connections for objects on different threads.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    TorAn (6th September 2016)

  8. #7
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unsure about QThreadPool object ownership

    Thanks! It would be very useful for me not to skip the documentation for the base classes . QObject docs talks about that very issue.

Similar Threads

  1. QThreadPool - how to remove all threads
    By ZioCanguro in forum Qt Programming
    Replies: 8
    Last Post: 6th September 2016, 19:58
  2. QThreadPool search threads
    By Cerberus in forum Newbie
    Replies: 5
    Last Post: 8th September 2015, 11:50
  3. Use QRunnable without QThreadPool
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2011, 11:02
  4. QEventLoop and QThreadPool Shutdown Problems
    By shawno in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2010, 19:49
  5. QThreadPool and QRunnable
    By jimc1200 in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 11:43

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.