QNetworkAccessManager from ThreadPool

2019-02-23 18:14发布

A very fundamental question. The documentation mentions that all methods in QNetworkAccessManager are reentrant. If so, is performing a get() method in a QRunnable without locks legal? My code would look something like this:

class MyClass: public QRunnable
{
    void run()
    {
        ...
        QNetworkAccessManager nam;
        QNetworkReply* reply =  name.get(request)    // No Read-write lock.
        ...
    }
};

2条回答
迷人小祖宗
2楼-- · 2019-02-23 18:52

From the Qt documentation:

[...] a class is said to be reentrant if its member functions can [simultaneously] be called safely from multiple threads, as long as each thread uses a different instance of the class.

Since you're using a different instance each time (the one you create on the stack in run()), you're on the safe side.

查看更多
叛逆
3楼-- · 2019-02-23 18:58

As a side note to this ,if you just want the GET request to be asynchronous, QNetworkAccessManager is already asynchronous (says so in the docs).

查看更多
登录 后发表回答