QNetworkAccessManager get/post from different thread possible?

孤人 提交于 2019-12-18 08:55:15

问题


I have a single QNetworkAccessManager object (as Qt docs recommend). However, I need to obtain a request from another thread, i.e. not the thread created the QNetworkAccessManager object.

This questions has two aspects:

  1. As the functions (get, post ... ) are not marked threadsafe I assume that I need to use a lock before calling them.
  2. But even if I make sure there are no 2 threads calling in parallel, issues could arise: So it could happen a QObject parent child relationship is set, but from objects in different threads. For that I would need to know the internals of QNetworkAccessManager

So is it allowed to call get/post from another thread?


回答1:


Is QNetworkAccessManager get/post calls from different threads possible?

I found QNetworkAccessManager from ThreadPool discussed here some time ago.

And because QNetworkAccessManager Class reference says:

All functions in this class are reentrant.

And the reentrance explained in 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.

So, the answer to this original question is: for QNetworkAccessManager to be safe for multiple calls from different threads you need one class instance per thread.



来源:https://stackoverflow.com/questions/35684123/qnetworkaccessmanager-get-post-from-different-thread-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!