Results 1 to 11 of 11

Thread: multithreading programming in Qt

  1. #1
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default multithreading programming in Qt

    I have a class A, I want to run A::function() in a new thread.
    After generate a new thread class MyThread: public QThread, I can override run funtion.
    But how can I use the A::function in the run() function? I need to use a lot of variables defined in class A.
    If I generate a signal in MyThread to run A::function, is it in another thread? or in the main thread, since after generate the signal, the new thread finishes it's job.
    Thanks a lot for your opinion.
    Last edited by Sheng; 27th October 2008 at 15:25.

  2. #2
    Join Date
    Oct 2008
    Location
    Kharkov, Ukraine
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by Sheng View Post
    I have a class A, I want to run A::function() in a new thread.
    After generate a new thread class MyThread: public QThread, I can override run funtion.
    But how can I use the A::function in the run() function? I need to use a lot of variables defined in class A.
    If I generate a signal in MyThread to run A::function, is it in another thread? or in the main thread, since after generate the signal, the new thread finishes it's job.
    Thanks a lot for your opinion.
    All code, before you start thread by QThread::start, run in the thread-creator of MyThread object (including constructor etc.). Code, listed in MyThread::run in separate thread (after calling start()). But you still have access to ALL data and methods in MyThread, because all threads share all data and code (don't forgot separate access to shared data by mutexes/semaphores)

    PS
    Sorry on my English.....

  3. #3
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by defender View Post
    All code, before you start thread by QThread::start, run in the thread-creator of MyThread object (including constructor etc.). Code, listed in MyThread::run in separate thread (after calling start()). But you still have access to ALL data and methods in MyThread, because all threads share all data and code (don't forgot separate access to shared data by mutexes/semaphores)

    PS
    Sorry on my English.....
    I understand I can access all data and methods in MyThread, but can I access variables in class A?
    Let me explain my question more clearly, In class A, I have function(),
    void A::function()
    {
    }
    and a MyThread object
    MyThread thread;

    If I override MyThread::run(), how can I call A::function? Apparently I can not call it directly and I have to use signal, but I am wondering if I use signal, A::function() will run in main thread instead of a secondary thread.

  4. #4
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by Sheng View Post
    I understand I can access all data and methods in MyThread, but can I access variables in class A?
    Let me explain my question more clearly, In class A, I have function(),
    void A::function()
    {
    }
    and a MyThread object
    MyThread thread;

    If I override MyThread::run(), how can I call A::function? Apparently I can not call it directly and I have to use signal, but I am wondering if I use signal, A::function() will run in main thread instead of a secondary thread.
    OK, I think I get the answer when I read the detailed information of QThread class.
    "With direct connections, the slot gets called immediately when the signal is emitted. The slot is executed in the thread that emitted the signal (which is not necessarily the thread where the receiver object lives).", so the A::function() should be run in a secondary thread.
    That's what I want, just add a fifth parameter Qt:: DirectConnection in the connect function defined in class A.
    Last edited by Sheng; 27th October 2008 at 17:15.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading programming in Qt

    Quote Originally Posted by Sheng View Post
    I understand I can access all data and methods in MyThread, but can I access variables in class A?
    It depends. If you ask is it possible to call the method then yes, this is possible - all you need is a pointer to the object and you can call its method as usual. If you ask if it is safe to do it, then the answer depends on what the method does and how the A class is designed. In that case read about reentrancy and thread-safety.

  6. The following user says thank you to wysota for this useful post:

    Sheng (28th October 2008)

  7. #6
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by wysota View Post
    It depends. If you ask is it possible to call the method then yes, this is possible - all you need is a pointer to the object and you can call its method as usual. If you ask if it is safe to do it, then the answer depends on what the method does and how the A class is designed. In that case read about reentrancy and thread-safety.
    Thanks, you are right. But I think it is dangerous to do that, even when A is singleton class, it might cause infinite recursive call in some case.

  8. #7
    Join Date
    Oct 2008
    Location
    Kharkov, Ukraine
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by Sheng View Post
    Thanks, you are right. But I think it is dangerous to do that, even when A is singleton class, it might cause infinite recursive call in some case.
    Let's see.
    I have class A. It implement some calculate process:

    Qt Code:
    1. class A {
    2. public:
    3. A();
    4. void setData(const sometype __d);
    5. void calc();
    6. private:
    7. sometype data;
    8. QMutex data_mutex;
    9. };
    10.  
    11. void A::setData(const sometype __d) {
    12. if(data_mutex.tryLock()) // error to user;
    13. data=__d;
    14. data_mutex.unlock();
    15. }
    16. void A::calc() {
    17. if(!data_mutex.tryLock()) {
    18. QMessageBox::warning(NULL,"Lock","Data is still locked!");
    19. return;
    20. }
    21. while(1) {
    22. ///calculate
    23. }
    24. data_mutex.unlock();
    25. }
    To copy to clipboard, switch view to plain text mode 
    Can show to me on an example of this class, what sort of a problem can arise? Also what you understand as infinite recursive calls? You can loss data? Two (or more) threads can go into conditional race?
    Please, specify peace of code where you plan to use such classes.
    Last edited by wysota; 28th October 2008 at 17:11. Reason: Changed [qtclass] to [code]

  9. The following user says thank you to defender for this useful post:

    Sheng (28th October 2008)

  10. #8
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by defender View Post
    Let's see.
    I have class A. It implement some calculate process:

    class A {
    public:
    A();
    void setData(const sometype __d);
    void calc();
    private:
    sometype data;
    QMutex data_mutex;
    };

    void A::setData(const sometype __d) {
    if(data_mutex.tryLock()) // error to user;
    data=__d;
    data_mutex.unlock();
    }
    void A::calc() {
    if(!data_mutex.tryLock()) {
    QMessageBox::warning(NULL,"Lock","Data is still locked!");
    return;
    }
    while(1) {
    ///calculate
    }
    data_mutex.unlock();
    }
    Can show to me on an example of this class, what sort of a problem can arise? Also what you understand as infinite recursive calls? You can loss data? Two (or more) threads can go into conditional race?
    Please, specify peace of code where you plan to use such classes.
    I mean, in general, this kind of design might cause problems. But if you say whether we can do it or not, we certainly can.
    a simple example:

    class A
    {
    public:
    A();
    ~A();
    void functionA();
    void run();

    };

    class B
    {
    public:
    B();
    ~B();
    void functionB();
    A* a;
    };

    void A::functionA()
    {cout<<"functionA"<<endl;
    B* b=new B;
    b->functionB();
    delete b;
    }

    void A::run()
    { B* b=new B;
    b->functionB();
    delete b;
    }

    A::A()
    {
    }

    A::~A()
    {
    }


    B::B()
    {
    a=new A;
    }

    B::~B()
    {
    }

    void B::functionB()
    {
    cout<<"functionB"<<endl;
    A* a=new A;
    a->functionA();
    delete a;
    }

    int main()
    {
    B b;
    b.a->run();
    }

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading programming in Qt

    Quote Originally Posted by defender View Post
    Can show to me on an example of this class, what sort of a problem can arise?
    If you use a mutex then you are safe when it comes to race conditions. But why are you using tryLock() instead of lock()? Currently you indeed can lose data as the lock may fail if another thread is already in the critical section and your data won't be saved. What is dangerous in the above code is the call to QMessageBox from a worker thread.

  12. #10
    Join Date
    Oct 2008
    Location
    Kharkov, Ukraine
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multithreading programming in Qt

    Quote Originally Posted by wysota View Post
    If you use a mutex then you are safe when it comes to race conditions. But why are you using tryLock() instead of lock()? Currently you indeed can lose data as the lock may fail if another thread is already in the critical section and your data won't be saved. What is dangerous in the above code is the call to QMessageBox from a worker thread.
    If we imaging, what class A is the GUI thread and class MyThread -- is calculate thread, then using lock, instead of tryLock, will block GUI thread. QMessageBox -- is a simple example. We must replace it with an error handler.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading programming in Qt

    If you lock for long enough for your user to notice it, then yes. But then you should probably change your design. If you lock only to set some value, don't worry about it. I'd suggest changing the design a little. For instance introduce the usage of signals and slots - then you don't have to do any locking at all.

Similar Threads

  1. Windows programming in Qt (serial communication)
    By bnilsson in forum General Programming
    Replies: 17
    Last Post: 9th February 2010, 18:17
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. QT COM Programming
    By sarav in forum Newbie
    Replies: 5
    Last Post: 24th February 2007, 13:41
  4. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  5. MODEL/VIEW programming
    By mira in forum Newbie
    Replies: 3
    Last Post: 21st April 2006, 11:19

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.