gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:54:40 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com