来自不同线程的 QNetworkAccessManager get/post 可能吗?

QNetworkAccessManager get/post from different thread possible?

我有一个 QNetworkAccessManager 对象(正如 Qt 文档所推荐的)。但是,我需要从另一个线程获取请求,即不是创建 QNetworkAccessManager 对象的线程。

这个问题有两个方面:

  1. 由于函数 (get, post ... ) 没有标记 threadsafe 我假设我需要在调用它们之前使用锁。
  2. 但即使我确保没有 2 个线程并行调用,也可能会出现问题:因此可能会发生 QObject 父子关系设置,但来自不同线程中的对象。为此,我需要了解 QNetworkAccessManager
  3. 的内部结构

那么是否允许从另一个线程调用 get/post?

Is QNetworkAccessManager get/post calls from different threads possible?

我发现 QNetworkAccessManager from ThreadPool 前段时间在这里讨论过。

并且因为 QNetworkAccessManager Class 参考资料说:

All functions in this class are reentrant.

重入在Reentrancy and Thread-Safety中解释:

... a class is said to be reentrant if its member functions can be called safely from multiple threads, as long as each thread uses a different instance of the class. The class is thread-safe if its member functions can be called safely from multiple threads, even if all the threads use the same instance of the class.

因此,这个原始问题的答案是:QNetworkAccessManager 为了保证来自不同线程的多次调用的安全,每个线程需要一个 class 实例。