从多个 tcp 连接读取数据 [英] Reading data from multiple tcp connection

查看:38
本文介绍了从多个 tcp 连接读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下您有 200 个检测器通过 tcp 套接字连接到您的程序的情况.他们非常频繁地发送他们的数据和我想尽可能高效地处理它.

Consider situation where you have 200 detectors that are connected to your program through tcp sockets. They are quite frequently sending their data and I would like to handle it as efficiently as possible.

我可以想到解决这个问题的 2 种方法,但我对 QT 很陌生,所以我不知道哪个更好(如果有的话).

I can think of 2 approaches for this problem, but I'm quite new in QT so I don't know which one is better, if any.

  1. 创建一个线程池,该线程池将运行 200 个从 QRunnable() 派生的对象,每个对象将包含一个套接字和将连接到该套接字信号的插槽,以便有关一个检测器的所有数据都将在那个对象.(在它的 run() 方法中会有 QEventLoop)

  1. Create a threadpool that will be running 200 objects derived from QRunnable(), each object will consist of a socket and slots that will be connected to this socket signals, so that all data concerning one detector will be handled in that one object. (In it's run() method there will be QEventLoop)

创建 200 个对象,每个对象由 socket 组成,并将这 200 个 socket 信号连接到主线程中的一个插槽.因此它将在一个插槽中处理来自 200 个检测器的数据.

Create 200 objects, each object will consist of socket and connect those 200 socket signals to one slot in the main thread. So It will handle data from 200 detectors in one slot.

考虑到在第一种方法中将创建 200 个 QEventLoops(对于每个对象),哪种方法会更好?

Which approach would be better consdering the fact that in the first approach there will be created 200 QEventLoops (for each object)?

推荐答案

在所有情况下,您都不希望线程数超过逻辑处理器内核数.跨线程分布对象.使用旋转事件循环的 QRunnable 是相当没有意义的,即使我承认在某人的请求的 SO 回答中证明了它.Eventloops 也不便宜——每个都需要几千字节的堆栈,至少在我的平台上是这样.因此,最好只使用 QThread,每个线程都有一个事件循环,然后以循环方式在线程之间分发网络对象.

In all cases, you don't want more threads than there are logical processor cores. Distribute the objects across the threads. Using a QRunnable that spins an eventloop is fairly pointless, even if I admit to demonstrating it in a SO answer on someone's request. Eventloops aren't cheap either - each takes a few kilobytes of stack, at least on my platform. Thus it's better to just use QThread that has a single eventloop per thread, and then distribute the network objects across the threads in a round-robin fashion.

这篇关于从多个 tcp 连接读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆