Results 1 to 6 of 6

Thread: Problem with QtConcurrent::run

  1. #1
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with QtConcurrent::run

    Hello.
    I want to have variable number of threads in my application depends of settings.
    I am using QtConcurrent::run because then I can run any function of any class.
    I did something like this:
    Qt Code:
    1. QVector<QFuture<void> > asda;
    2. for(i=0; i<n; i++)
    3. {
    4. asda[i] = QtConcurrent::run(this, &MainWindow::thr1);
    5. }
    6. for(i=0; i<n; i++)
    7. {
    8. asda[i].waitForFinished();
    9. }
    To copy to clipboard, switch view to plain text mode 
    but when it is runing MainWindow::thr1 for the second time, then I get aplication error and:
    Qt Code:
    1. app.exe exited with code -1073741819
    To copy to clipboard, switch view to plain text mode 
    my header file:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <mailbox.h>
    6. #include <QStandardItem>
    7. #include <QStandardItemModel>
    8. #include <QtCore>
    9. #include <QFileDialog>
    10.  
    11.  
    12. namespace Ui {
    13. class MainWindow;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26. void upd(int numer, int percent, int speed);
    27.  
    28. void on_pushButton_3_clicked();
    29.  
    30. void on_pushButton_2_clicked();
    31.  
    32. private:
    33. Ui::MainWindow *ui;
    34. std::string generate();
    35. mailbox *mailb[8];
    36. void thr1();
    37. QString dir;
    38. void download();
    39. };
    40.  
    41. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    and MainWindow::thr1 function is just for testing:
    Qt Code:
    1. void MainWindow::thr1()
    2. {
    3. qDebug() << "test";
    4. }
    To copy to clipboard, switch view to plain text mode 
    What am I doing wrong?
    I would be grateful, if someone could help.
    Janusz

  2. #2
    Join Date
    Jul 2011
    Posts
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4

    Default Re: Problem with QtConcurrent::run

    The asda vector is not correctly initialized, you have to set the size before using the operator[]

  3. #3
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QtConcurrent::run

    thanks, I replace = with pushback and it is working now. But I have one more question, how can I close the threads when my application is closing, if i don't wait for finished? Because when I am closing application, just window is closing.

  4. #4
    Join Date
    Jul 2011
    Posts
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4

    Default Re: Problem with QtConcurrent::run

    One possible solution is to add a flag in your class to see if the user wants to quit the application and deals with it directly in your function thr1 to abort the computation.

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

    januszmk (29th July 2011)

  6. #5
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QtConcurrent::run

    thanks for help

  7. #6
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QtConcurrent::run

    I get one more problem with QtConcurrent::run, When I run this function:
    Qt Code:
    1. void MainWindow::download()
    2. {
    3. QItemSelection selection( ui->tableView->selectionModel()->selection() );
    4. foreach( const QModelIndex & index, selection.indexes() )
    5. {
    6. rows.append( index.row() );
    7. }
    8. down1 = rows.count();
    9. if(ui->lineEdit_3->text().isEmpty())
    10. {
    11. on_pushButton_3_clicked();
    12. }
    13. watek.clear();
    14. for(down=0; down<8; down++)
    15. {
    16. connect(mailb[down], SIGNAL(progress(int,int,int)), this, SLOT(upd(int,int,int)));
    17. connect(mailb[down], SIGNAL(done(int)), this, SLOT(asda(int)));
    18. QString temp1 = ui->tableView->model()->data(ui->tableView->model()->index(rows[down],0)).toString();
    19. QString temp2 = ui->tableView->model()->data(ui->tableView->model()->index(rows[down],1)).toString();
    20. mailb[down]->number = rows[down];
    21. mailb[down]->number1 = down;
    22. QString type = mailb[down]->type.c_str();
    23. temp1 = ui->lineEdit_3->text() + "/" + temp1;
    24. qDebug() << temp1 << temp2;
    25. watek.push_back(QtConcurrent::run(mailb[down], &mailbox::downloadFile, temp2, temp1));
    26. //mailb[0]->downloadFile(temp2, temp1);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    function downloadFile is run just twice, not 8 times. Is there maybe some limits for number of threads? If there are, then how can I bypass that limits?

    Edit:
    I found then QtConcurrent is using QThreadPool and I add "QThreadPool::globalInstance()->setMaxThreadCount(10);" and now it's working
    Last edited by januszmk; 30th July 2011 at 19:30.

Similar Threads

  1. QtConcurrent::mappedReduced
    By wookoon in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2010, 08:53
  2. QtConcurrent, i need advice
    By SABROG in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 19:53
  3. Why is QtConcurrent so slow?
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 10:37
  4. qtconcurrent
    By knishaq in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2009, 08:41
  5. QtConcurrent
    By Brandybuck in forum An Introduction to QThreads
    Replies: 2
    Last Post: 9th May 2008, 14:10

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.