我可以在属于主线程的 QThread 中使用 waitForReadyRead 吗?

发布于 2024-08-22 23:30:50 字数 285 浏览 3 评论 0原文

我有一个包含 QUDPsocket 的 QThread (套接字不是 QThread::run() 本地的成员,也许我应该更改我正在阅读的内容)。这个 QThread 在我的 QMainWindow 类中实例化,即 GUI 线程(我没有调用移动到线程)。使用 waitForReadyRead 是否仍然安全,或者我绝对需要在 main.cpp 中实例化 QThread 或调用 moveToThread() 才能保证线程安全。我在以目前的方式调用 waitForReadyRead 时遇到间歇性的双重释放异常(有时我几天都没有得到它,有时在 3 分钟后)。

I have a QThread that contains a QUDPsocket (socket is member not local to QThread::run(), maybe I should change that from what I am reading). This QThread is instantiated in my QMainWindow class ie the GUI thread(I am not calling move to thread). Is it still safe to use waitForReadyRead or do I absolutly need to instantiate the QThread in main.cpp or call moveToThread() for it to be thread safe. I am getting intermittent double free exception inside the call to waitForReadyRead in the present way of doing it(sometimes I dont get it for days sometimes after 3 minutes).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-08-29 23:30:50

请查看 Qt 文档,了解 <代码>QUdpSocket。那里有一条注释解释该类是可重入的。同样来自 Qt 文档 :

...如果一个类的成员函数可以从多个线程安全地调用,并且每个线程使用该类的不同实例,则该类被认为是可重入的。

因此,要回答您的问题,QThread 的父级是什么并不重要,只要您确保您正在使用的 QUdpSocket 实例在其中实例化即可您正在使用它的线程的上下文。

Have a look at the Qt documentation for QUdpSocket. There is a note there explaining the class is reentrant. Also from the Qt documentation:

...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.

Thus, to answer your question, it does not really matter what the parent of the QThread is, as long as you make sure that the QUdpSocket instance you are using is instantiated within the context of the thread you are using it in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文