如果 Qt 从同一信号调用两个插槽,它们可以同时调用吗?
如果两个不同线程中有两个槽,并且这些槽连接到第三个线程中的信号。两个插槽是否会同时被信号调用,或者它们每次都被同步调用?
我问这个问题是因为我想发送一些回调数据结构(用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 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.
看看这里(官方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.