我正在用 QT 做多线程程序。
我使用此代码来尝试它是否按我预期的那样工作。
QFuture<void> t1 = QtConcurrent::run(thread_process1, (void *)this);
QFuture<void> t2 = QtConcurrent::run(thread_process2, (void *)this);
并且 thread_process1 和 2 都只是一行
qDebug()<<"thread id: "<<QString("%1").arg((int) QThread::currentThreadId(), 0, 16) ;
然而,它们都显示
线程 ID:“ffffffffb6085b40”
我做错了吗??
QFutureWatcher 似乎没有帮助。
最佳答案
运行的文档说,
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.
不能保证每次调用 run 都会在不同的线程中运行。有可能这些函数运行得如此之快,它们都由同一个线程顺序处理。尝试在 thread_process_1 中放入 sleep 调用,以查看这些函数是否被不同的线程接收。
关于multithreading - QtConcurrent 运行的线程 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344650/