QFuture<void> Thread is not Starting
-
@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()
? -
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); }
-
-
@KroMignon said in QFuture<void> Thread is not Starting:
pool.setMaxThreadCount(2);
@Ketan__Patel__0011
It's interesting. If that is indeed needed (I don't doubt it is), https://doc.qt.io/qt-5/qthreadpool.html#maxThreadCount-prop :The default maxThreadCount is QThread::idealThreadCount().
https://doc.qt.io/qt-5/qthread.html#idealThreadCount :
Returns the ideal number of threads that can be run on the system. This is done querying the number of processor cores, both real and logical, in the system. This function returns 1 if the number of processor cores could not be detected.
OOI, how many cores do you actually have? What OS are you?
-
@Ketan__Patel__0011
It would be interesting to hear/confirm whatstatic int QThread::idealThreadCount()
returns on your system?But to resolve your issue, you should be following what @KroMignon said above,
pool.setMaxThreadCount(2);
. Confirm that allows yourFunction_2()
to run? -
@JonB said in QFuture<void> Thread is not Starting:
static int QThread::idealThreadCount()
Where i call this function ?
-
Christian Ehrlicher Lifetime Qt Championreplied to Ketan__Patel__0011 on last edited by Christian Ehrlicher
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
Where i call this function ?
It's a static function - therefore you can call it from everywhere - c++ basics. And there is still no minimal, compilable example here btw...
-
I know Static methods can call everywhere
but in my case run more than two threads
so is this functions return number of cores or number of running threads count ?
-
@JonB said in QFuture<void> Thread is not Starting:
pool.setMaxThreadCount(2);
Yes this is working but when I stop this two thread and again try to start that time Function_2() Is not starting. Only Function_1() Is Start Successfully.
-
@JonB said in QFuture<void> Thread is not Starting:
static int QThread::idealThreadCount()
This Function is return the total core for your system
i my system have 4 coreso this function return me 4