[Qt-interest] QT signal and reentrant

Luca Ferrari fluca1978 at infinito.it
Tue Jun 16 12:50:33 CEST 2009


On Tuesday 16 June 2009 11:46:09 am 邵度's cat walking on the keyboard wrote:
> Hello,
>
> I am a newbie of QT. Let me starts from the following code segment:
>
> int* getPower(int i)
>   {
>   static int result;
>   result = (int)pow(2, i);
>   sleep(5);
>   return &result;
>   }
>
> void handler (int signal_number)
>   {
>   getPower(3);
>   }
>
> int main ()
>   {
>   int *result;
>   struct sigaction sa;
>   memset(&sa, 0, sizeof(sa));
>   sa.sa_handler = &handler;
>   sigaction(SIGUSR1, &sa, NULL);
>   result = getPower(5);
>   printf("2^5 = %d\n", *result);
>   return 0;
>   }
>
> After executing above program, if I send the signal USR1 to it within 5
> seconds, the output will display "2^5 = 8" as its result.
> This error is due to the behavior while receiving signals in UNIX.
> My question is: will the same situation occur while emiting a QT signal?
> Any why?
> I can't figure it out from QT documentations even if I study it many times.
> Can anyone explain me the detail QT signal/slot mechanism?

Unix signals are a  lot different from qt signals. The former are pending 
messages that are executed as soon as the scheduler detects them, while the 
latter are more like "events" dispatched by an always running thread. Emitting 
a signal in qt means that there will be a method call to the slot method with 
the specified parameters, so in your case if the slot sleeps the dispatching 
thread will too and the signal will be delivered sequentially when the thread 
wakes up. Sleeping upon an incoming event is another issue....

Hope this helps.
Luca





More information about the Qt-interest-old mailing list