如果 Qt 从同一信号调用两个插槽,它们可以同时调用吗?

发布于 2024-12-05 07:17:27 字数 143 浏览 4 评论 0原文

如果两个不同线程中有两个槽,并且这些槽连接到第三个线程中的信号。两个插槽是否会同时被信号调用,或者它们每次都被同步调用?

我问这个问题是因为我想发送一些回调数据结构(用QSharedPointer封装)并询问是否需要内部锁定机制。

If there are two slots in two different threads and these slots are connected to a signal in a third thread. Can it happen, that both slots get called at the same time by the signal or do they get called synchronized every time?

I ask because i want to send some callback data structure (encapsulated with QSharedPointer) and ask if locking mechanism inside is needed.

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

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

发布评论

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

评论(2

愚人国度 2024-12-12 07:17:27

如果您使用 Qt::QueuedConnection 将信息传递给线程,则不需要锁定实际的信号/槽调用,因为 QueuedConnection 机制以线程安全的方式处理此问题。

话虽这么说,您仍然需要保护线程访问的任何共享内存,无论它们是如何调用的。第三个线程发出单个信号以导致两个槽被调用的事实不会改变这一点。

You don't need to lock the actual signal/slot calls if you're using a Qt::QueuedConnection to pass the information to your threads, as the QueuedConnection mechanism handles this in a thread-safe manner.

That being said, you still need to protect any shared memory your threads access, regardless of how they were called. The fact that a third thread emitted a single signal to cause both slots to be called will not change this.

哭泣的笑容 2024-12-12 07:17:27

看看这里(官方Qt文档Qt 关于线程的信号/槽机制)。

每个插槽都在其线程内调用,因此我很确定任何事情都可能发生。您应该安装锁定装置。

Have a look here (official Qt documentation for Qt's signal/slot mechanism regarding threads).

Each slot is called inside its thread, therefore I am pretty sure anything can happen. You should install a lock mechanism.

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