Question

I am doing multi-thread programs with QT.

I use this code to try whether it acts as i expected.

QFuture<void> t1 = QtConcurrent::run(thread_process1, (void *)this);
QFuture<void> t2 = QtConcurrent::run(thread_process2, (void *)this);

and both thread_process1 and 2 are only one line which is

qDebug()<<"thread id: "<<QString("%1").arg((int) QThread::currentThreadId(), 0, 16) ;

however, they both show thread id: "ffffffffb6085b40"

am I do it wrong?? QFutureWatcher seems to do no help.

Was it helpful?

Solution

The docs for run say,

Runs function in a separate thread. The thread is taken from the global QThreadPool. Note that the function may not run immediately; the function will only be run when a thread is available.

There is no guarantee that each call to run will run in a different thread. It is possible that the functions run so quickly they are both handled sequentially by the same thread. Try putting in a sleep call in thread_process_1 to see if the functions are then picked up by different threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top