asynchronous communication between threads

Charles Hixson charleshixsn at earthlink.net
Tue Jan 1 15:54:31 PST 2013


If I were to use the below as an asynchronous communication channel, 
would it avoid deadlocks (presuming that only Cell called Msg) and that 
when a thread activated Cell, the first thing it did was process it's 
mailbox?
Also, if only around 7 cells were created in the main thread, would RAM 
usage remain reasonable (i.e., when a thread was through with an MCell, 
would only one copy continue to exist in RAM?  Or would there be a copy 
for each thread that had ever referenced it?  Should MCell instances be 
marked shared?

FWIW, I would expect there to be many thousands of instances of MCell, 
and around 7 threads processing them.  Cells would only interact with 
other cells via the mailbox, which could only receive messages, except 
that the associated cell could also remove messages from it.

class	MCell
{
    struct	Msg
    {  int[]	fromId;
       string[]	msg;
       MCell[]   from;

    }
	
    synchronized	class	MBox
    {  Msg[]	msg;
       void  receive (MCell mcell, int idNo, string messg)
       {  int  last = msg.length;
	 msg.length = last + 1;
          msg[last].fromId  = idNo;
          msg[last].msg     = messg;
          msg[last].from    = mcell;
       }

    }
	
    synchronized	class	Cell
    {  int	idNo;
		
       MCell	next;
       MCell[]	up;
       MCell[]	down;

       void send (MCell dest, string msg)
       {   dest.receive (this, idNo, msg);  }
    		
    }
}



More information about the Digitalmars-d-learn mailing list