i use a for loop to send request to http://www.example.com
:
for(int i = 0; i < size; ++i)
{
request.setUrl("http://www.example.com");
manager.get(request);
}
and i want to handle the reply parallel, so i run 10 threads, each threads has a slot function to add the reply to a Qvector<QNetworkReply*>
. i connect this 10 slot function to the signal finished(QNetworkReply*)
of manager
. in this case, when the finished(QNetworkReply*)
emitted, this 10 slot function will called to add this reply to the vector 10 times. i want to if a thread has handle the function, and the rest of the 9 threads drop this signal to wait another signal emitted. is there a method to eat the finished(QNetworkReply*)
in the first slot?
http://en.wikipedia.org/wiki/Thread_pool_pattern
I think what you are looking for is a Thread Pool to handle requests. A bunch of workers waiting to do the same thing, and once there is a request to handle it.
https://doc.qt.io/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
Hope that helps.