Results 1 to 13 of 13

Thread: cancel slot not getting called with QtConcurrent::run()

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default cancel slot not getting called with QtConcurrent::run()

    I am trying to implement the multi threading programming.

    Prob: I need to read huge data from Db (Which will be done through 3rd party API for which i don't have control), So my plan is so create ideal number of threads (max 5)
    and each thread reads chunk of data at the end all the chunks will be merged. (I should be able to cancel the process in middle if I don't want).

    I just tried below sample code(Before going to above implementation), when I click on cancel button It's slot is not get called, instead slot will be getting called after all the threads finished.

    Qt Code:
    1. // I am just doing some busy work here As I don't know what is happening here, this function is not in my control
    2. QVector<int> thirdPartyFunction()
    3. {
    4. QTime time;
    5. int t1 = time.elapsed()/1000;
    6.  
    7. while( ((time.elapsed()/1000) - t1) < 3)
    8. {
    9.  
    10. }
    11. return QVector<int>();
    12. }
    13.  
    14. QVector<int> myThread()
    15. {
    16. return thirdPartyFunction();
    17. }
    18.  
    19.  
    20. Dialog::Dialog(QWidget *parent) : QDialog(parent)
    21. {
    22. progress = new QProgressDialog("Threading in progress", "Cancel", 0, 100, this);
    23. connect(progress, SIGNAL(canceled()), this, SLOT(cancelAllThreads()), Qt::QueuedConnection);
    24.  
    25. progress->setValue(0);
    26.  
    27. // starting 5 threads
    28. for(int i=0; i< 5; i++)
    29. {
    30. synch.addFuture(QtConcurrent::run(myThread));
    31. progress->setValue(progress->value() + 10);
    32. }
    33.  
    34. synch.waitForFinished();
    35. QVector<int> finalList;
    36.  
    37.  
    38. if(synch.cancelOnWait())
    39. {
    40. qDebug() << "cancel all the threads......";
    41. }
    42. else
    43. {
    44. //if I never press cancel
    45. QList<QFuture< QVector<int> > > futuresList = synch.futures();
    46.  
    47. for ( int i = 0; i < futuresList.size(); i++ )
    48. {
    49. QVector<int> list = futuresList.at(i);
    50. finalList << list;
    51.  
    52. progress->setValue(progress->value() + 10);
    53. qDebug() << "pro val = " << progress->value() + 10;
    54. }
    55. qDebug() << "All threads finished successfully - size" << finalList.size();
    56. }
    57. }
    58.  
    59. Dialog::~Dialog()
    60. {
    61. }
    62.  
    63. //this is not getting called as soon as I licked cancel
    64. void Dialog::cancelAllThreads()
    65. {
    66. //here I want to cancel & clean up all the threads
    67. qDebug() << "Canceled pressed....";
    68. synch.setCancelOnWait(true);
    69. }
    To copy to clipboard, switch view to plain text mode 

    Now difficulties I am facing are:

    1. GUI is not responding even if I click cancel. So cancelAllThreads() is not getting called (GUI thread is freezing some how),
    this function is getting called when all the threads are finished even I cancel in middle.
    2. I am not able to update progress bar values properly (As I don't have control over thirdPartyFunction, so while creating thread i am showing 50% &
    when all the threads finished another 50% but this will give bad experience to the user as showing 50% at start and wait for long long time and then suddenly 100%).
    3. Qt doc said I can not cancel QtConcurrent::run(..), is there a way to stop/kill this thread in middle
    (doc said can not cancel but looking for some smart idea if possible) ?


    What am I doing wrong here??
    Thanks :-)

  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: cancel slot not getting called with QtConcurrent::run()

    You are blocking the UI thread in waitForFinished().

    Cheers,
    -

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    But I need to synchronise all the result at the end so I am just waiting till all the threads finished their computations.. any other way to do it or wait without blocking main thread.
    Thanks :-)

  4. #4
    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: cancel slot not getting called with QtConcurrent::run()

    QFutureWatcher can be used to be notified about a QFuture's completion.

    Cheers,
    _

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

    prasad_N (28th June 2015)

  6. #5
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    Thanks. What about canceling these threads, is there away to stop this threads in middle once started ??
    Thanks :-)

  7. #6
    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: cancel slot not getting called with QtConcurrent::run()

    Not without cooperation of the threads.
    The threads need to check for a exit/cancel condition and then cease doing their work.

    Maybe you can split the tasks in a series of subtasks and then peform such a check between each step.

    Cheers,
    _

  8. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    I can not do it as series of action. The reason i switched to threading is I need to decrease the reading time as much as posible.
    Thanks :-)

  9. #8
    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: cancel slot not getting called with QtConcurrent::run()

    So you can split it into 5 units of work an no further?
    What are those 5 tasks?

    Cheers,
    _

  10. #9
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    If suppose I need to read 5 lak rows of data from DB, I am starting 5 threads so that each thread read 1 lak rows, at the end I will merge the result.
    The thing is that reading records is from third party function so I do not have control over it(I don't know what is happening inside, how much time will take, I cant not put any bool variable inside it to stop at particular condition ...).
    Thanks :-)

  11. #10
    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: cancel slot not getting called with QtConcurrent::run()

    So the third party API only allows you to read 1 lak rows, no smaller amount?

    I.e. you can't read 50 times 0.1 lak rows?

    Cheers,
    _

  12. #11
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    Quote Originally Posted by anda_skoa View Post
    So the third party API only allows you to read 1 lak rows, no smaller amount?
    I can read any number, just I need to pass boundaries (start row & end row Indexes).

    I.e. you can't read 50 times 0.1 lak rows?
    I can, But what is the use of this approach reading 50 times 0.1 lak rows? (Sorry I may not get your logic)

    Note: We did some experiment and find out that more that 8 threads are slowing down the performance on my machines.
    So we are running max 8 threads to to read entire data.
    Thanks :-)

  13. #12
    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: cancel slot not getting called with QtConcurrent::run()

    Quote Originally Posted by prasad_N View Post
    I can read any number, just I need to pass boundaries (start row & end row Indexes).
    Then you can split the work into smaller steps, which you said you could not (comment #7)

    Quote Originally Posted by prasad_N View Post
    I can, But what is the use of this approach reading 50 times 0.1 lak rows? (Sorry I may not get your logic)
    Smaller units of work take less time, waiting for such a smaller amount of time on cancel makes the program react to cancel faster.

    Quote Originally Posted by prasad_N View Post
    Note: We did some experiment and find out that more that 8 threads are slowing down the performance on my machines.
    So we are running max 8 threads to to read entire data.
    I already thought that 5 threads are pretty optimistic, but I am not suggesting increasing the number of threads at all.

    Cheers,
    _

  14. #13
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cancel slot not getting called with QtConcurrent::run()

    Quote Originally Posted by anda_skoa View Post
    Then you can split the work into smaller steps, which you said you could not (comment #7)_
    yes, it could have been I don't want instead I cann't :-)


    Quote Originally Posted by anda_skoa View Post
    Smaller units of work take less time, waiting for such a smaller amount of time on cancel makes the program react to cancel faster._
    I will try this and get back with the result, I need to check how much time it will take to read entire DB (My main aim is to minimize reading time).

    Quote Originally Posted by anda_skoa View Post
    I already thought that 5 threads are pretty optimistic, but I am not suggesting increasing the number of threads at all._
    I am working on SLES11 server(high end machine), more that 5 threads might works in my case.
    Thanks :-)

Similar Threads

  1. Slot never called
    By Kola73 in forum Newbie
    Replies: 2
    Last Post: 27th June 2015, 09:48
  2. Replies: 6
    Last Post: 12th February 2015, 13:20
  3. Replies: 2
    Last Post: 26th August 2011, 09:51
  4. Slot gets called twice
    By greatgatsby in forum Newbie
    Replies: 7
    Last Post: 20th August 2009, 16:11
  5. SLOT not being called
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2007, 12:30

Tags for this Thread

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.