简体   繁体   English

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

[英]Can Qt call two slots simultaneously, if they get called from the same signal?

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. 我问是因为我想发送一些回调数据结构(用QSharedPointer封装),并询问是否需要内部锁定机制。

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. 如果您使用Qt :: QueuedConnection将信息传递给线程,则无需锁定实际的信号/插槽调用,因为QueuedConnection机制以线程安全的方式处理此问题。

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. 第三个线程发出一个信号导致两个插槽都被调用的事实不会改变这一事实。

Have a look here (official Qt documentation for Qt's signal/slot mechanism regarding threads). 在这里看看(有关线程的Qt信号/插槽机制的官方Qt文档)。

Each slot is called inside its thread, therefore I am pretty sure anything can happen. 每个插槽都在其线程内调用,因此我很确定会发生任何事情。 You should install a lock mechanism. 您应该安装一个锁定机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM