我使用 for 循环将请求发送到 http://www.example.com
:
for(int i = 0; i < size; ++i)
{
request.setUrl("http://www.example.com");
manager.get(request);
}
我想并行处理回复,所以我运行 10 个线程,每个线程都有一个槽函数来将回复添加到 Qvector<QNetworkReply*>
.我将这个 10 槽函数连接到信号 finished(QNetworkReply*)
的 manager
.在这种情况下,当 finished(QNetworkReply*)
发出后,将调用此 10 槽函数以将此回复添加到 vector 中 10 次。我想如果一个线程已经处理了这个函数,其余 9 个线程会丢弃这个信号以等待另一个信号发出。 finished(QNetworkReply*)
有没有吃的方法在第一个插槽?
最佳答案
http://en.wikipedia.org/wiki/Thread_pool_pattern
我认为您正在寻找的是一个线程池来处理请求。一堆 worker 等着做同样的事情,一旦有请求就去处理。
http://qt-project.org/doc/qt-5/qthreadpool.html#details
The
QThreadPool
class manages a collection ofQThread
s.
QThreadPool
manages and recyles individualQThread
objects to help reduce thread creation costs in programs that use threads. Each Qt application has one globalQThreadPool
object, which can be accessed by callingglobalInstance()
.To use one of the
QThreadPool
threads, subclassQRunnable
and implement therun()
virtual function. Then create an object of that class and pass it toQThreadPool::start()
.
https://www.google.com/search?q=qthreadpool+network
http://www.bogotobogo.com/Qt/Qt5_QTcpServer_QThreadPool_Multithreaded_Client_Server.php
希望对您有所帮助。
关于c++ - 有没有一种方法可以将 Qt 中的信号设为 `eat`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277630/