PDA

View Full Version : One question about the execution of the slot function



charlse
24th April 2012, 08:10
Hi guys,
I have a class Test, and there is one normal function funcA(), and a slot function slot():
void Test::funcA()
{
if(m_index != 2)
{
//---------------- interrupt
//DO SOMETHINS
}
}

slot:
void Test::slot()
{
m_index = 2;
}

And now I have a question, If when I call function funcA(), and there is a signal which associate with slot: slot() emited, just after execute "if(m_index != 2)" and enter the if state, will the funcA() be interrupted at "---------------- interrupt" and begin to run slot()? if yes, there will be a unpredictable fault!

Thanks for your suggestions!

Lesiok
24th April 2012, 11:09
Only when the function and the slot are executed in separate threads. In this case, you need to ensure synchronization ie. with QMutex.

Spitfire
25th April 2012, 13:06
Even if signal is emitted from separate thread, the slot() will not execute untill thread running funcA() will return to the event loop.

And as far as I know there's no way to put two functions of the same object in separate threads :)

Lesiok
26th April 2012, 11:21
Even if signal is emitted from separate thread, the slot() will not execute untill thread running funcA() will return to the event loop.

And as far as I know there's no way to put two functions of the same object in separate threads :)
Of course You can. QObject::connect method have parameter Qt::ConnectionType. You can create direct connection between two threads. Then slot is executed immediately. Not discuss whether it makes sense but it is possible.