QFuture<void> Thread is not Starting
-
@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
-
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
so is this functions return number of cores or number of running threads count ?
So hard to read the documentation?
-
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
so this function return me 4
In that case the
pool.setMaxThreadCount(2);
should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think. -
@JonB said in QFuture<void> Thread is not Starting:
In that case the pool.setMaxThreadCount(2); should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think.
This was just a example to ensure calling booth QtConcurrent:run() will always work, because the used thread pool will always have enough thread to start them.
Of course you can allocate more threads to this pool, but that is not the point for this issue.
My understanding of your issue is that the thread pool you are using does not have free thread to start the new concurrent function.@Ketan__Patel__0011 have you tried what I have suggested? Does it work?
-
Thanks for You Reply
Yes I was try you suggested answer and it is working
but some time when I try to start both function that time Second function is not run
and this problem is not continuously raised
it raised sometime
for example
try to start and stop both function again and again -
@Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:
try to start and stop both function again and again
As written in documentation, to start a new function there must be an available thread in the used thread pool:
Runs function in a separate thread. The thread is taken from the global QThreadPool. Note that function may not run immediately; function will only be run once a thread becomes available.
You could check before starting function how many thread are already used:
qDebug() << "Allowed thread:" << pool.maxThreadCount() <<"/ threads in use:" << pool.activeThreadCount() << "/ available threads:" << (pool.maxThreadCount() - pool.activeThreadCount());