Mailbox limits and dead/livelocks?

Enerqi kelvin.d.ward at googlemail.com
Tue Jul 24 07:46:23 PDT 2012


The actors in std.concurrency - in this example it's 
non-deterministic whether the process may or may not hang. I'm 
confused as to why. I have set the mailbox sizes to 1. But the 
message flow seems simple enough. I don't see how the message 
limit of 1 is blocking everything, I am catching Variant messages 
so nothing should be in the mailbox unread.


----------
import core.thread;
import std.algorithm;
import std.concurrency;
import std.stdio;

int main(string[] args)
{
     Tid[2] tids;
     bool[Tid] response_flags;
     for(auto i=0; i < 2; ++i)
     {
	tids[i] = spawn(&blah1, thisTid);		
         response_flags[tids[i]] = false;
     }

     auto receive_count = 0;
	
     setMaxMailboxSize(thisTid, 1, OnCrowding.block);
	
     while(! reduce!"a && b"(response_flags))
     {
	writeln(response_flags, " ", receive_count);
         receive((Tid some_blah_id, int done)
                 {					
                     assert(done == 42);
                     ++receive_count;
                     response_flags[some_blah_id] = true;					
                 },
                 (Variant v)
                 {
                     writeln("Unexpected message: ", v, " type ", 
v.type());
                 }
                 );		
     }

     return 0;
}

void blah1(Tid parent)
{
     setMaxMailboxSize(thisTid, 1, OnCrowding.block);
     auto subtid = spawn(&subsub, thisTid, parent);

     send(subtid, thisTid, false);
     receive((bool got)
	    {
	        assert(got);
	    },
	    (Variant v)
	    {
                 writeln("Unexpected message: ", v, " type ", 
v.type());
             });
     send(subtid, thisTid, 10);
     receive((int got)
	    {
	        assert(got == 11);
	    },
	    (Variant v)
	    {
                 writeln("Unexpected message: ", v, " type ", 
v.type());
             });
}

void subsub(Tid parent, Tid grandparent)
{
     setMaxMailboxSize(thisTid, 1, OnCrowding.block);
     receive((Tid parent, bool whatever)
             {
	        assert(!whatever);
	    },
	    (Variant v)
	    {
                 writeln("Unexpected message: ", v, " type ", 
v.type());
             });
     send(parent, true);
     receive((Tid parent, int n)
             {
                 assert(n == 10);
	    },
	    (Variant v)
	    {
                 writeln("Unexpected message: ", v, " type ", 
v.type());
             });
     send(parent, 11);

     send(grandparent, parent, 42);
}

----------


Cheers.


More information about the Digitalmars-d-learn mailing list