Results 1 to 20 of 21

Thread: qt multi threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default qt multi threads

    Hello,everry one.
    I want do the following things with qt multi threads.But I do not know how to realize it?
    main ()
    {
    .....
    create thread1// do thing A
    create thread 2// do thing B
    do thing C.
    ......
    }
    I want to the thing C does after the A and B both have finished.When the program run,C did before A and B finish(because thread1 and thread2 have not finished), how Could I realize what I want to do?

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

    Default Re: qt multi threads

    What's the reason for using threads then? If you insist on using threads, use wait conditions or mutexes to synchronize them. Threads A and B will release respective mutexes and only then thread C will do its job. If you don't want to block thread C (if it's your GUI thread) then you have to use another approach. In that case read this article:
    http://doc.trolltech.com/qq/qq27-responsive-guis.html
    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.


  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt multi threads

    Thank you for your answer.

    1.The reason for using thread.
    Qt Code:
    1. main()
    2. {
    3. ...
    4. for(i=0;i<max;i++)
    5. {
    6. // there are a lot things will be done in this circle,and it will consume much time.
    7. }
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    In order to decrease the time,I want to do it with multi -threads.
    Qt Code:
    1. for(i=0;i<max/2;i++)
    2. //threads A does
    3. for(i=max/2+1;i<max;i++)
    4. //threads B does.
    To copy to clipboard, switch view to plain text mode 
    2.When I create a class like this.
    Qt Code:
    1. class A: public QThread
    2. {
    3. void run()
    4. {
    5. QMessageBox::warning(0,"dddd","ddddd",0,0,1);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    run the progam,the following error will occur:
    "Widgets must be created in the GUI thread.", file kernel/qwidget.cpp,
    I know I do not inherit from gui widget,but if I write like this
    Qt Code:
    1. class A: public QWidget,public QThread
    2. {
    3. void run()
    4. {
    5. QMessageBox::warning(0,"dddd","ddddd",0,0,1);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    error still exists.
    How could I do with it?
    Last edited by wysota; 5th April 2009 at 08:56. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    You have to create Widgets in main (GUI) thread, so instead of
    Qt Code:
    1. QMessageBox::warning(...);
    To copy to clipboard, switch view to plain text mode 
    use qDebug() to print to console or, for example, notify GUI thread via signal/slot about what you want to display as a QMessageBox and the GUI thread will do that in slot.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    are you sure u'd need 3 different threads to accomplish your tasks..using too many threads can be overkill and as explained in the article by wysota, will most of the times bring the performance of ur application down..try to use just one extra thread and see how ur app performs..to make sure u dont start task C before A and B..start an event loop in main()..let A and B be done in one thread..when they are done, emit a signal, which is caught in the main thread upon which the eventloop is quitted..then perform task C..

    Qt Code:
    1. Class A&B : public QThread
    2. {
    3. protected run()
    4. {
    5. TaskA();
    6. TaskB();
    7. emit done();
    8. }
    9. signals:
    10. done();
    11. }
    12.  
    13. main thread:
    14. int main(...)
    15. {
    16. //start thread A&B
    17. QEventLoop loop;
    18. connect(threadObject, SIGNAL(done()), &loop, SLOT(quit()));
    19. loop.exec();
    20. taskC();
    21. ....
    22. }
    To copy to clipboard, switch view to plain text mode 

    if u read wysota's article carefully, u might have already figured that out by now.

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

    Default Re: qt multi threads

    Quote Originally Posted by weixj2003ld View Post
    In order to decrease the time,I want to do it with multi -threads.
    Qt Code:
    1. for(i=0;i<max/2;i++)
    2. //threads A does
    3. for(i=max/2+1;i<max;i++)
    4. //threads B does.
    To copy to clipboard, switch view to plain text mode 
    This is wrong. Either the for loops need to be inside the threads or you have to change your architecture. Otherwise you're losing computing power. I suggest you use QtConcurrent to manage multithreading for you - the runnables solution is something you want to have a look at.
    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.


  7. #7
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt multi threads

    Thanks for answer.
    1. Now,I create 2 threads,the thread class like this:
    mythread.h
    Qt Code:
    1. class Mythread:public QThread
    2. {
    3. Mythread::Mythread(int start,int end,MyclassA *a)
    4. void MyThread::run()
    5. {
    6.  
    7. }
    8. public:
    9. MyclassA *a1;
    10. int start1,end1;
    11. }
    12. mythread.cpp
    13. Mythread::Mythread(int start,int end,MyclassA *a)
    14. {
    15. start1=start;
    16. end1=end;
    17. a1=a;
    18. }
    19. Mythread::run()
    20. {
    21. for(i=start1;i<end1;i++)
    22. {
    23. //do task.Myclass will be used here.
    24. }
    25. }
    26. main.cpp
    27. {
    28. ...
    29. MyclassA myclassa is created.
    30. .....
    31. Mythread *thread1=new Mythread(0,10,myclassa);
    32. Mythread *thread2=new Mythread(11,30,myclassa);
    33. thread1.start();
    34. thread2.start();
    35. ...
    36. }
    To copy to clipboard, switch view to plain text mode 
    2.If I only create one thread(thread1,for example) and the program is ok,but if I create 2 threads,when I run the program,it will crash.Why?
    Last edited by wysota; 5th April 2009 at 16:56. Reason: missing [code] tags

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    you shouldnt ignore what different replies you get in the posts..your case is a classic example for which Qt Concurrent is applicable..read about it and try to implement it..u wont have to use those multiple threads..Qt will take care of everything without letting anything crash..

  9. #9
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt multi threads

    Thank you for answering my question patiently.
    My program built with qt4.3.1,and a lot of things have been done by other people,I am afraid I can not change it to upper version.but Q4.3.1 does not support Qt Concurrent.
    I think I will manage the threads by myself ,but not Qt Concurrent.
    My questions:
    1.What should I do if I want to let Q4.3.1 support Qt Concurrent?
    2. Can data type( class defined by user) be shared by threads?
    Thank you.

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

    Default Re: qt multi threads

    Quote Originally Posted by weixj2003ld View Post
    Thank you for answering my question patiently.
    My program built with qt4.3.1,and a lot of things have been done by other people,I am afraid I can not change it to upper version.but Q4.3.1 does not support Qt Concurrent.
    Why can't you upgrade Qt?

    2. Can data type( class defined by user) be shared by threads?
    Not without proper synchronization. If you do synchronize using a mutex, then it can be shared. Some of the classes will be safe without synchronization but it depends on a case by case basis. And in general widgets can't be shared between threads, regardless if you do the synchronization or not.
    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.


  11. #11
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt multi threads

    A class(defined by myself) shared by the threads must be lock and unlock?

  12. #12
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    yes, using mutex

  13. #13
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt multi threads

    when I used two threads like what I have said in my program and compile and run it,it will be crashed at radom position,Is it because of my not synchronizing my threads?Must I synchronize them?

  14. #14
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    if there is a shared object, u have to make it thread-safe..and yes, synchronize..and again, why cant u upgrade Qt? u have a solution waiting for u in there, so i cant understand ur reluctance

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. i'm in troubles withe threads !!
    By jiboon in forum Newbie
    Replies: 3
    Last Post: 21st April 2008, 20:36
  4. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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.