Results 1 to 20 of 20

Thread: Help me about multithread!

  1. #1
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Help me about multithread!

    I am programming with multithread. In my program, I create a thread, how to wait this thread is finish before execute continue. Please see my simple program:

    #include <QApplication>
    #include <QThread>

    class MyThread : public QThread
    {
    public:
    MyThread();
    void run();
    };

    MyThread::MyThread() : QThread()
    {
    }

    void MyThread::run()
    {
    qWarning("Thread running.");
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyThread *thread = new MyThread;
    thread->start();
    qWarning("Output this line after thread finished.");
    return 1;
    }

    Please modify my program to it output:

    Thread running.
    Output this line after thread finished.


    Thanks a lot.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help me about multithread!

    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Use a mutex that is unlocked by the thread when it is done.

  4. #4
    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: Help me about multithread!

    Why use a thread in such a situation? If the main thread is idle, why not use it to do the other thread's job?

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Hi,
    Why use a thread in such a situation? If the main thread is idle, why not use it to do the other thread's job?
    I supose that "vql" is learnig how to work with threads.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. MyThread *thread = new MyThread;
    5. thread->start();
    6. thread->wait(); //Here the main thread will stop until your thread exits the run method
    7. qWarning("Output this line after thread finished.");
    8. return 1;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

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

    vql (3rd February 2007)

  7. #6
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Ah, I programming with multithread, this program is very big. So I give a simple program to ask everybody. Thank NyAw' help. It solve my simple program.

    In the big program, I use multithread. So I modify with 2 threads as following:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyThread *thread1 = new MyThread;
    thread1->start();
    thread1->wait();
    MyThread *thread2 = new MyThread;
    thread2->start();
    thread2->wait();
    qWarning("Output this line after threads finished.");
    return 1;
    }

    In this program, threre are 3 threads: thread1, thread2, mainthread. When call thread1->wait(), only mainthread wait thread1 finish or both of thread2 and mainthread wait thread1 finish.

    At here, I want both thread1 and thread2 run at the same time, and they mustn't wait together. And They finish before call qWarning("Output this line after threads finished.");

    My problem is that in my big program, there are a lot threads, I start them and want them finish before I call any commands after that. Thanks. Please help me. Example: I create two threads to get value to memory. I run them but at this time, I don't know it finish or not. Because, I need they finish before I operate with values get from threads.

  8. #7
    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: Help me about multithread!

    wait() blocks the thread that called it, so if you want both threads to run simoultaneously, you have to call wait() only when you called start() on both threads:
    Qt Code:
    1. MyThread t1, t2;
    2. t1.start();
    3. t2.start();
    4. t1.wait();
    5. t2.wait();
    To copy to clipboard, switch view to plain text mode 

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

    vql (3rd February 2007)

  10. #8
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    If I use your code:
    Qt Code:
    1. MyThread t1, t2;
    2. t1.start();
    3. t2.start();
    4. t1.wait();
    5. t2.wait();
    To copy to clipboard, switch view to plain text mode 

    When t1.wait() called, thread t2 will wait t1 finish or it run simoultaneously with t1.

  11. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help me about multithread!

    Both threads will run simultaneously.
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    vql (3rd February 2007)

  13. #10
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Thank you. My problem is solved. And I have another question. If I don't declare thread as class variable. When I create a new thread. Do I know how many threads running in my program. There are a lot threads, how to make mainthread in idle status?

  14. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help me about multithread!

    In Qt a single QThread object represents a single thread. So obviously you want to store the pointers to those threads somewhere. It's a question of design where to keep them.

    Anyway, the main thread is "idle" and able to handle events and such as long as it's running an event loop and you are not for example waiting another thread (calling QTread::wait() blocks until one of the conditions is met. See QThread::wait() docs for explanation of those "conditions".

    I'd like to refer back to the question by wysota:
    Quote Originally Posted by wysota View Post
    Why use a thread in such a situation? If the main thread is idle, why not use it to do the other thread's job?
    J-P Nurmi

  15. #12
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    You don't understand my problem. I'm programming 3D using Coin library. I use thread to draw faster. Example: I want to draw Truck, Vessel to display on a form.

    If I don't use thread:
    Qt Code:
    1. truck.draw();
    2. vessel.draw();
    3. form.show();
    To copy to clipboard, switch view to plain text mode 
    Form will display truck & vessel.

    But If i use thread:
    Qt Code:
    1. truckthread.start(); // call truckthread.run(){draw()};
    2. vesselthread.start(); // call vesselthread.run(){draw()};
    3. form.show();
    To copy to clipboard, switch view to plain text mode 

    suppose at the time form.show() called, truckthread finished but vesselthread not finish. Form will only display truck. I want both truck & vessel displayed. Please solve it for me.

    Finally, when use multithread, we must call start() for all threads, after that we must call wait() for all threads so that they finished before we call any commands after? Please view my tree:

    Qt Code:
    1. thread1.start();
    2. thread2.start();
    3. // ...
    4. threadn.start();
    5.  
    6. thead1.wait();
    7. thead1.wait();
    8. // ...
    9. theadn.wait();
    10.  
    11. // you code after threads finished. At here, all threads finished?
    To copy to clipboard, switch view to plain text mode 

  16. #13
    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: Help me about multithread!

    Why do you say it'll display faster if you use threads? Have you actually verified that? As far as I know Coin uses OpenGL to do its job, which would suggest that if you're drawing a single scene using a single GL context in two or more threads without any synchronisation, you'll be asking for trouble.

    Do you have a multiprocessor machine? Because if not, threads will only slow down your application.

  17. #14
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Please answer this first:
    when use multithread, we must call start() for all threads, after that we must call wait() for all threads so that they finished before we call any commands after? Please view my tree:
    Qt Code:
    1. thread1.start();
    2. thread2.start();
    3. // ...
    4. threadn.start();
    5.  
    6. thead1.wait();
    7. thead1.wait();
    8. // ...
    9. theadn.wait();
    10. // you code after threads finished. At here, all threads finished?
    To copy to clipboard, switch view to plain text mode 
    This's right?

    As you say, in Coin, we should use thread or not? My machine is multiprocessor.
    Please answer that question.
    Because, my program is 3D, have a lot trucks to draw (>1000), so I use object Truck as a thread to draw.
    As you know, in Coin, I declare a variable SoSeparator *_root;
    with each Truck, I call _root->addChild(truck->draw()).
    After that, I need all of truck finish drawing to _root contain all nodes from trucks.
    And call _examinerviewer->setSceneGraph(_root);
    But because of truck is a thread, If I initialize 1000 Trucks to draw; there are some trucks not finish before call setSceneGraph.
    Can you make a suggestion for my program? Thanks alot.

  18. #15
    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: Help me about multithread!

    Quote Originally Posted by vql View Post
    when use multithread, we must call start() for all threads, after that we must call wait() for all threads so that they finished before we call any commands after?
    This's right?
    Depends what you want. But if you want to block the flow until all the threads have finished their work, then that's correct. But it's highly inappropriate if your application is a GUI application - in such situation you should use signals and slots instead.

    As you say, in Coin, we should use thread or not?
    "Should"? No... "Can"? Possibly... First of all you'd have to make sure Coin3D is fully reentrant.


    Can you make a suggestion for my program?
    Forget about threads in all situations regarding display (rendering as well). You can use them (if you feel like it) for computation intensive fragments. For example you can do all computation intensive tasks in one thread and handle the gui in the other. But there is no point in spawning 1000 threads just to make a simple task. Thread spawning takes time, so everything you might gain from having threads run simoultaneously, you're likely to lose during thread initialisation.

    I suggest you do some experiments if possible gains are worth the effort.

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

    vql (4th February 2007)

  20. #16
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Forget about threads in all situations regarding display (rendering as well).
    I don't understand clearly. Does it relative with my example:
    Qt Code:
    1. truckthread.start(); // call truckthread.run(){draw()};
    2. vesselthread.start(); // call vesselthread.run(){draw()};
    3. form.show();
    To copy to clipboard, switch view to plain text mode 
    We can't use threads in this case, because I want truck & vessel drawn on form.

  21. #17
    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: Help me about multithread!

    If you can't use threads then why do you use them? I suggested you to stop using threads and you ask how is it related to your problem because you can't use threads?

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

    vql (4th February 2007)

  23. #18
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Thank you very much, wysota.
    Finally. I want to ask wysota a question about Coin3D.
    I can using Coin + SoQt + MingW (not Visual Studio) on Windows? I tried but failed when compiling Coin with MingW. Thanks.

  24. #19
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me about multithread!

    Anyone can give me a example about thread (use with classes QThread, QMutex, QMutexLocker, or QWailCondition). It's very complex when in my program, must combine above classes. Because, examples in Qt Assistant is very simple.

    Thanks.

  25. #20
    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: Help me about multithread!

    What exactly is that you want to achieve?

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.