C++ QObject:无法使用QtConcurrent::run为位于不同线程中的父线程创建子线程
我试图在qt中使用线程,但在我使用的代码中出现了一些奇怪的错误 我使用的函数是线程C++ QObject:无法使用QtConcurrent::run为位于不同线程中的父线程创建子线程,c++,multithreading,qt,qthread,qtconcurrent,C++,Multithreading,Qt,Qthread,Qtconcurrent,我试图在qt中使用线程,但在我使用的代码中出现了一些奇怪的错误 我使用的函数是线程 QThreadPool::globalInstance()->setMaxThreadCount(size); QFutureSynchronizer<void> synchronizer; for(quint64 i=0;i<size;i++) { synchronizer.addFuture(QtConcurrent::run(this, &Market::ticke
QThreadPool::globalInstance()->setMaxThreadCount(size);
QFutureSynchronizer<void> synchronizer;
for(quint64 i=0;i<size;i++)
{
synchronizer.addFuture(QtConcurrent::run(this, &Market::tickerForThread,markets[permission[i]],QString("%1%2/value").arg(url).arg(permission[i])));
}
synchronizer.waitForFinished();
还有我犯的错误
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xad4490)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xacc3c0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xacbd90)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xad4b20)
错误的原因是什么?如何解决?
谢谢你的回复
错误的原因是什么?如何解决
父母和孩子必须生活在同一条线上。解决方法是将它们分别移动到同一个线程中
您要么需要将QNAM实例移动到子线程,要么需要移动子线程。如果不进一步澄清上下文,我无法告诉您比这更具体的建议。laszlo,我使用了tickerForThread中的get函数来减速新的QNetworkAccessManager,但对我来说仍然太慢。我需要10-20个与QNetworkAccessManager同步我如何处理它?使用多个QNetworkAccessManager?是的,我终于解决了这个问题。但我改变了我不再使用的类的结构QtConcurrent::run,因为QNetworkAccessManager已经有了自己的线程。@VeyselBekirMacit:太好了,那么我的答案缺少什么?@VeyselBekirMacit,啊,好的。:)这么简单完美的解释!非常感谢。
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xad4490)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xacc3c0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xacbd90)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xa64d80), parent's thread is QThread(0x6f9270), current thread is QThread(0xad4b20)