Results 1 to 2 of 2

Thread: connect and qthread

  1. #1
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default connect and qthread

    I'm trying to make a multithreaded pi calculation program.

    It's working ok but my coding seems horribly messy (and no doubt 'wrong' in some sense).

    I'd appreciate someone having a look and suggesting a better way of doing it.

    The following gets executed upon a button press:

    Qt Code:
    1. void MainWindow::findpifunc()
    2. {
    3.  
    4.  
    5. tabstack = new QVector<pi_display_box*>; //a vector containing a number of tabs (to view the pi calculation) // pi_display_box is a customised qtextedit
    6. threadstack = new QVector <QThread*>; //a vector containing a number of threads
    7. objstack = new QVector <Thr_object*>; //a vector of corresponding objects
    8.  
    9. for (int corestorun=0;corestorun<core_box->currentText().toUInt();corestorun++) //for the number of cores to run the calculation upon
    10. {
    11. tabstack->append(new pi_display_box); //add display widget to stack
    12.  
    13. threadstack->append(new QThread); //add a new qthread
    14.  
    15. objstack->append(new Thr_object); //add a new object
    16.  
    17. pi_bar->addTab(tabstack->at(corestorun),"Core " + QString::number(corestorun+1)); //opens new tab with display widget
    18. }
    19.  
    20.  
    21. for (int corestorun=0;corestorun<core_box->currentText().toUInt();corestorun++)
    22. {
    23. objstack->at(corestorun)->moveToThread(threadstack->at(corestorun)); //move the object to the relevant thread
    24.  
    25. connect (threadstack->at(corestorun),SIGNAL(started()),objstack->at(corestorun),SLOT(process())); //connect the started signal to process where the thread execution occurs
    26.  
    27. //when thread finishes
    28. connect (objstack->at(corestorun),SIGNAL(finished()),threadstack->at(corestorun),SLOT(quit())); //deleting thread etc
    29. connect (objstack->at(corestorun),SIGNAL(finished()),objstack->at(corestorun),SLOT(deleteLater()));
    30. connect (threadstack->at(corestorun),SIGNAL(finished()),threadstack->at(corestorun),SLOT(deleteLater()));
    31.  
    32.  
    33.  
    34. objstack->at(corestorun)->settextdisplay(tabstack->at(corestorun),pidigits_box->currentText()); //a few arguments loaded into the object
    35. threadstack->at(corestorun)->start(); //begin the code execution
    36. }
    37.  
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. The following user says thank you to tom989 for this useful post:


  3. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect and qthread

    how does each Thr_object know how not to repeat work already done?

    Why are you using user input for core count instead of something like QThread::idealThreadCount() ?

    Do you know you have memory leaks?
    Last edited by amleto; 6th May 2013 at 00:00.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. The following user says thank you to amleto for this useful post:


Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. Replies: 2
    Last Post: 9th May 2011, 10:38
  3. Replies: 16
    Last Post: 16th February 2010, 13:17
  4. Replies: 4
    Last Post: 26th June 2008, 18:41
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.