QFuture<void> Thread is not Starting
-
Hello Friends And QT Experts And Champions
My New Problem is when I start to run my functions parallelly that time one function in not starting
My Code Is :
void MainWindow::StartFunctions() { QFuture<void> future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully QFuture<void> future_2 = QtConcurrent::run(this, &MainWindow::Function_2); /// This Function in not Starting For Processing }
When i call this function that time my future_1 thread is started but future_2 is not starting
Please help me solve this problem
Thanks in advance
-
Take from QConcurrent::run() documentation:
Note that function may not run immediately; function will only be run once a thread becomes available.
I would suggest you to use a dedicated thread pool to ensure to functions would be started in parallel.
Add aQThreadPool
member to yourMainWindow
class and ensure it has enough threads for you parallel works:pool.setMaxThreadCount(2);
And change your code:
void MainWindow::StartFunctions() { future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); }
Another improvement would be to check function is not already running:
void MainWindow::StartFunctions() { if(!future_1.isRunning()) future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); if(!future_2 .isRunning()) future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); }
-
How do you check / know that the function is not started? Please provide a minimal, compilable example to show your problem.
-
@Christian-Ehrlicher
Thanks for replyvoid MainWindow::Function_1() { qDebug() << "START:_Function_1"; CheckBool_1 = true; while(CheckBool_1) { QThread::msleep(100); qDebug() << "Function 1 \t Function 1"; } qDebug() << "END:_Function_1"; } void MainWindow::Function_2() { qDebug() << "START:_Function_2"; CheckBool_2 = true; while(CheckBool_2) { QThread::msleep(100); qDebug() << "Function 2 \t Function 2"; } qDebug() << "END:_Function_2"; } void MainWindow::StartFunctions() { QFuture<void> future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully QFuture<void> future_2 = QtConcurrent::run(this, &MainWindow::Function_2); /// This Function in not Starting For Processing } void MainWindow::on_BtnStart_clicked() //// START { StartFunctions(); } void MainWindow::on_BtnStop_clicked() //// Stop { CheckBool_1 = false; if(future_1 ->isRunning()) { future_1->setPaused(true); future_1->cancel(); } CheckBool_2 = false; if(future_2->isRunning()) { future_2->setPaused(true); future_2->cancel(); } }
-
@Ketan__Patel__0011 This can NOT work, and I pretty sure this would also not build.
You have declared
future_1
andfuture_2
as local variable inMainWindow::StartFunctions()
! -
Thanks for reply
i was already try with global -
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
i was already try with global
Why global?!
-
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
i was already try with global
I don't want to blame you, but what are your C/C++ skills?
I would also not use globals but class members. -
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
Because I want to access it anywhere in application
Sounds like bad design...
-
@Ketan__Patel__0011 Why do you need access to these variables everywhere in your program?
Make these variables members of the class and if other parts of the application REALLY need access to them implement getter methods returning pointers to these variables... -
@Ketan__Patel__0011 Do you mean you changed MainWindow::StartFunctions() ? Because in the code you posted before both variables were local variables.
-
@Ketan__Patel__0011 And does it work now or not?
-
Let Me Share My Whole Code
mainwindow.h
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: void Function_1(); void Function_2(); void StartFunctions(); private slots: void on_BtnStart_clicked(); void on_BtnStop_clicked(); private: bool CheckBool_1; bool CheckBool_2; Ui::MainWindow *ui; QFuture<void> future_1 ; QFuture<void> future_2 ; };
mainwindow.cpp
void MainWindow::StartFunctions() { future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully future_2 = QtConcurrent::run(this, &MainWindow::Function_2); /// This Function in not Starting For Processing } void MainWindow::on_BtnStart_clicked() //// START { StartFunctions(); } void MainWindow::on_BtnStop_clicked() //// Stop { CheckBool_1 = false; if(future_1 ->isRunning()) { future_1->setPaused(true); future_1->cancel(); } CheckBool_2 = false; if(future_2->isRunning()) { future_2->setPaused(true); future_2->cancel(); } }
-
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
Sorry to say it's not working
What do you mean with "not working"?
As far as I can see from your code, onFunction_1()
start, you should see "START:_Function_1" on debug output and "START:_Function_2" onFunction_2()
start.Do you have any of those message on debug console?
-
i didn't get "START:_Function_2" this message on my debug console.
-
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
i didn't get "START:_Function_2" this message on my debug console.
And when you invert the lines in
StartFunctions()
to startFunction_2()
beforeFunction_1()
?