Signals and Slots

Sean Kelly sean at f4.ca
Fri Nov 3 08:17:28 PST 2006


Frits van Bommel wrote:
> 
> If we replace this line by:
>               {  slots_idx--;
>                  dg = slots[slots_index];
>                  slots[slots_idx] = null;
> then we don't need to check for empty slots in emit():
> If slots.length == slots_idx, we can be sure the array is full.
> 
> (Unless there's a requirement for the slots to be
> called in any particular order?)

No, but the sequence may need to be modified while emit is processing. 
Consider a slot that disconnects itself when it receives a signal.  But 
I suppose you could do something like this:

void emit( T1 i )
{
     {
         isEmitting = true;
         scope(exit) isEmitting = false;

         foreach( s; slots )
             s(i);
     }
     foreach( s; disconnects )
         disconnect( s );
}

void disconnect( slot_t s )
{
     if( !isEmitting )
     {
         // remove from slots
         return;
     }
     disconnects ~= s;
}

Things get even worse in a multithreaded app if the signal is shared and 
other threads can be connecting and disconnecting at any time.


Sean



More information about the Digitalmars-d mailing list