c++ - 有没有一种方法可以将 Qt 中的信号设为 `eat`?

标签 c++ multithreading qt

我使用 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 of QThreads.

QThreadPool manages and recyles individual QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global QThreadPool object, which can be accessed by calling globalInstance().

To use one of the QThreadPool threads, subclass QRunnable and implement the run() virtual function. Then create an object of that class and pass it to QThreadPool::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/

相关文章:

c++ - OpenCV 和 Eclipse CDT

c++ - 网格三角测量 : Seperate vs Nested loop Complexity

c++ - &&、基于范围的循环和临时变量如何协同工作?

java - 使用 Executor 服务时出现线程问题

ruby - ruby 有真正的多线程吗?

java - wait 和 notifyall 等待其他线程

android - Necessitas QT - 在设备上安装共享 QT 库

qt - 如何在Qt中用其他字母书写文本?

c++ - 临时住址?

c++ - 这个引用会失效吗?