multithreading - QtConcurrent 运行的线程 id

标签 multithreading qt qtconcurrent

我正在用 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/

相关文章:

multithreading - Kettle 6.0.1.0 中的 "Serial Single Threaded"转换引擎类型是什么?

c++ - CMake 与 Qt : moc creates . cpp_parameters 结束

用于读取和写入 opendocument 电子表格 (*.ods) 的 C++ 类

c++ - QByteArray : Is it safe to read from the array ('[ ]' ) between writes on the DataStream ('<<' )? 上的 QDataStream

python - PySide/PyQt 中的 QtConcurrent

c++ - 在多线程环境中使用 auto_ptr 交换对象而不锁定是安全的吗?

python - 中止 HTTP 请求跨线程

c# - 如何管理 ThreadPool 中的项目?

c++ - 如何卸载高度依赖于正确异常处理的计算繁重的任务?

c++ - 为将来的 QThreadPool 线程和/或 pthread_create 调用设置默认堆栈大小