b2ag
b2ag

Reputation: 53

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.

Upvotes: 4

Views: 1804

Answers (2)

Chris
Chris

Reputation: 17535

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.

Upvotes: 2

Benoît
Benoît

Reputation: 16994

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.

Upvotes: 0

Related Questions