Qt-interest Archive, February 2007
connecting signals and slots across different threads
Message 1 in thread
Hi all. Suppose I'm connecting a signal and slot betwen two objects
that are created on the main thread. However, "emit" is called for
the sender's signal on a different thread. Does this signal get
queued a la Qt::AutoConnection, or does the sender object also have
to be created in the thread from which emit is getting called?
The reason I'm asking, is because occasionally, if events occur in a
specific order, my UI hangs when i'm trying to emit this signal from
the secondary thread.
Thanks,
Bruno
Message 2 in thread
I tried creating the objects that send the signal in the secondary
thread, to no avail. Here are the two use-cases for when it works
fine or when the UI freezes:
For simplicity, I'm calling the object created in the secondary
thread SecondThread and the one on the main thread MainThread.
Good situation:
1. SecondThread emits signal.
2. SecondThread emits signal.
3. MainThread's slot entered.
4. MainThread emits signal to UI component.
5. MainThread's slot entered.
6. MainThread emits signal to UI component.
Bad situation (UI freezes):
1. SecondThread emits signal.
2. MainThread's slot entered.
3. MainThread emits signal to UI component.
4. SecondThread attempts to emit signal.
5. <FREEZE>
The freeze is happening on the call to emit.
On Feb 5, 2007, at 11:51 AM, Bruno Trindade wrote:
> Hi all. Suppose I'm connecting a signal and slot betwen two
> objects that are created on the main thread. However, "emit" is
> called for the sender's signal on a different thread. Does this
> signal get queued a la Qt::AutoConnection, or does the sender
> object also have to be created in the thread from which emit is
> getting called?
>
> The reason I'm asking, is because occasionally, if events occur in
> a specific order, my UI hangs when i'm trying to emit this signal
> from the secondary thread.
>
>
> Thanks,
> Bruno
>
>
Bruno Trindade
Señor Software Engineer
bruno@xxxxxxxxx
Message 3 in thread
Figured it out. Very silly - in some areas of code I was using
qDebug, and in others i was simply cout-ing some debug info. So in
the instances where these messages clashed, I would get the freeze.
On Feb 5, 2007, at 12:57 PM, Bruno Trindade wrote:
> I tried creating the objects that send the signal in the secondary
> thread, to no avail. Here are the two use-cases for when it works
> fine or when the UI freezes:
>
> For simplicity, I'm calling the object created in the secondary
> thread SecondThread and the one on the main thread MainThread.
>
> Good situation:
>
> 1. SecondThread emits signal.
> 2. SecondThread emits signal.
> 3. MainThread's slot entered.
> 4. MainThread emits signal to UI component.
> 5. MainThread's slot entered.
> 6. MainThread emits signal to UI component.
>
> Bad situation (UI freezes):
>
> 1. SecondThread emits signal.
> 2. MainThread's slot entered.
> 3. MainThread emits signal to UI component.
> 4. SecondThread attempts to emit signal.
> 5. <FREEZE>
>
>
> The freeze is happening on the call to emit.
>
>
>
>
> On Feb 5, 2007, at 11:51 AM, Bruno Trindade wrote:
>
>> Hi all. Suppose I'm connecting a signal and slot betwen two
>> objects that are created on the main thread. However, "emit" is
>> called for the sender's signal on a different thread. Does this
>> signal get queued a la Qt::AutoConnection, or does the sender
>> object also have to be created in the thread from which emit is
>> getting called?
>>
>> The reason I'm asking, is because occasionally, if events occur in
>> a specific order, my UI hangs when i'm trying to emit this signal
>> from the secondary thread.
>>
>>
>> Thanks,
>> Bruno
>>
>>
>
> Bruno Trindade
> Señor Software Engineer
> bruno@xxxxxxxxx
>
>
>
Bruno Trindade
Señor Software Engineer
bruno@xxxxxxxxx
Message 4 in thread
:) Ouch, usually when I find issues like this, I mutex lock everything
down tight, non-recursive, and gradually open things out from there.
That and tonne of qDebug() calls ;)
Bruno Trindade wrote:
> Figured it out. Very silly - in some areas of code I was using
> qDebug, and in others i was simply cout-ing some debug info. So in
> the instances where these messages clashed, I would get the freeze.
>
--
[ signature omitted ]
Message 5 in thread
On Monday 05 February 2007 17:51, Bruno Trindade wrote:
> Hi all. Suppose I'm connecting a signal and slot betwen two objects
> that are created on the main thread. However, "emit" is called for
> the sender's signal on a different thread. Does this signal get
> queued a la Qt::AutoConnection, or does the sender object also have
> to be created in the thread from which emit is getting called?
Yes, it does. AutoConnection will only call the slot directly if
QThread::currentThread() == sender->thread() == receiver->thread().
--
[ signature omitted ]