Results 1 to 7 of 7

Thread: Is this the correct way of using QConcurrent::run() >

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Is this the correct way of using QConcurrent::run() >

    Hello everyone,
    I am trying to make some parts of my app run in a different thread than th UI thread, meanwhile I am trying to use a timer to show the elapsed time on the form.
    For this I used the QConcurrent::run method like this :
    Qt Code:
    1. void frmCustomNetwork::on_btnTrain_clicked()
    2. {
    3. try
    4. {
    5. if( P.size() == 0 || T.size() == 0)
    6. {
    7. QMessageBox::warning(this,"No Sample for training is set","You need to specify some samples with their respective desired outputs first");
    8. return;
    9. }
    10.  
    11.  
    12. elapsedSeconds=0;
    13. timer->start();
    14. connect(&watcher,SIGNAL(finished()),this,SLOT(on_TimerTick()));
    15. future = QtConcurrent::run(this,&frmCustomNetwork::Rec,ui->chkPlot->isChecked());
    16. watcher.setFuture(future);
    17.  
    18. }
    19. catch(std::exception ex)
    20. {
    21. QMessageBox::critical(this,"Exception occured in on_btnTrain_clicked()",ex.what());
    22. }
    23. catch(...)
    24. {
    25. QMessageBox::critical(this,"Exception occured on_btnTrain_clicked()","Uknown Exception occured");
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    and this is the timerTick slot:
    Qt Code:
    1. void frmCustomNetwork::on_TimerTick()
    2. {
    3. elapsedSeconds++;
    4. if(watcher.isFinished())
    5. {
    6. timer->stop();
    7. elapsedSeconds = 0;
    8. return;
    9. }
    10.  
    11. day = elapsedSeconds / (24*3600);
    12. hour = (elapsedSeconds % (24*3600)) / 3600 ;
    13. min = ((elapsedSeconds % (24*3600)) % 3600) / 60;
    14. second = ((elapsedSeconds % (24*3600)) % 3600) % 60;
    15.  
    16. ui->lblElapsedTime->setText(QString::number(day)+" : "+QString::number(hour)+" : "+QString::number(min)+" : "+QString::number(second));
    17. }
    To copy to clipboard, switch view to plain text mode 

    the problem that I am facing now is :
    1.Is it the correct way of using QConcurent in first place?
    2.How can I destroy the running thread ( the one spawned by QConcurrect::run()) when the window closes ( that is when the Rec() is not finished yet and is being executed in a separate thread, I suddently decide to close the app)

    at the moment I am facing random crashes which I guess pretty much is because of the way I am creating and dealing with the new thread + timer.
    I would be grateful If anyone could give me a hand in this
    Thanks in advance

  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: Is this the correct way of using QConcurrent::run() >

    One option you have is to wait on the future or future watchers when exiting.
    Basically close the window and call watcher.waitForFinished() before exiting.

    Cheers,
    _

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

    Hossein (12th October 2015)

  4. #3
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Is this the correct way of using QConcurrent::run() >

    Quote Originally Posted by anda_skoa View Post
    One option you have is to wait on the future or future watchers when exiting.
    Basically close the window and call watcher.waitForFinished() before exiting.

    Cheers,
    _
    Thank you verymuch sir
    But this would continue to execute the method till it ends, right? and this is not a wanted outcome.
    suppose the user closes the app, thinking all is freed (resources I mean) and then tries to run or do whatever he was doing (possibly re executing the lengthy method )
    How can I get rid of that spawned thread immediately? Is there anyway for that?

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is this the correct way of using QConcurrent::run() >

    From QTimer doc : Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. I suggest using the class QElapsedTimer.

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

    Hossein (12th October 2015)

  7. #5
    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: Is this the correct way of using QConcurrent::run() >

    Quote Originally Posted by Hossein View Post
    How can I get rid of that spawned thread immediately? Is there anyway for that?
    QtConcurrent uses the global instance of QThreadPool, you don't have any control over which thread is being used.

    Stopping a thread requires that the code run by the thread regularily checks for a stop condition and a way for the main thread to make that stop condition to become true.

    In the very special case of exiting the application, it might be OK to call terminate(), but I recommend to avoid that until it is the last resort.

    Cheers,
    _

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

    Hossein (12th October 2015)

  9. #6
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Is this the correct way of using QConcurrent::run() >

    Quote Originally Posted by Lesiok View Post
    From QTimer doc : Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. I suggest using the class QElapsedTimer.
    thanks, but I cant use that, I need to update the time ( I need to show it to the user as well, and when I checked the documents, I couldn't find any events for QElapsedTimer that I can use instead of timers.

    Quote Originally Posted by anda_skoa View Post
    QtConcurrent uses the global instance of QThreadPool, you don't have any control over which thread is being used.

    Stopping a thread requires that the code run by the thread regularily checks for a stop condition and a way for the main thread to make that stop condition to become true.

    In the very special case of exiting the application, it might be OK to call terminate(), but I recommend to avoid that until it is the last resort.

    Cheers,
    _
    That method is atomic, I mean I cant put anything into that, Its a member function of of a class which I have no control over it.

  10. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is this the correct way of using QConcurrent::run() >

    Quote Originally Posted by Hossein View Post
    thanks, but I cant use that, I need to update the time ( I need to show it to the user as well, and when I checked the documents, I couldn't find any events for QElapsedTimer that I can use instead of timers.
    Of course You can. elapsedSeconds should be QElapsedTimer not int. Use QTimer only to update view not to count seconds.

  11. The following user says thank you to Lesiok for this useful post:

    Hossein (12th October 2015)

Similar Threads

  1. Replies: 30
    Last Post: 14th March 2015, 10:11
  2. this code is correct, but It will not run ?
    By rezas1000 in forum Newbie
    Replies: 1
    Last Post: 3rd September 2014, 11:52
  3. QConcurrent and images added onscreen
    By ErrMania in forum Newbie
    Replies: 0
    Last Post: 24th November 2011, 20:12
  4. Is this sentence correct?
    By SWEngineer in forum Newbie
    Replies: 6
    Last Post: 21st June 2011, 01:56
  5. QConcurrent::Run vs QThread
    By Andrea Landi in forum Newbie
    Replies: 1
    Last Post: 31st May 2011, 09:55

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.