Tid is not a process id?

Enerqi kelvin.d.ward at googlemail.com
Tue Jul 24 06:48:06 PDT 2012


Hi

I'm working with std.concurrency. I can't seem to get messages to 
reliably go to the right place.

------------
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;

     while(! reduce!"a && b"(response_flags))
     {
         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());
                     assert(false);
                 }
                 );
     }

     return 0;
}

void blah1(Tid parent)
{
     auto subtid = spawn(&subsub, thisTid, parent);

     send(subtid, thisTid, false);
     receive((bool got){ assert(got);});
     send(subtid, thisTid, 10);
     receive((int got){ assert(got == 11);});
}

void subsub(Tid parent, Tid grandparent)
{
     receive( (Tid parent, bool whatever){ assert(!whatever); });
     send(parent, true);
     receive( (Tid parent, int n){ assert(n == 10); });
     send(parent, 11);

     send(grandparent, parent, 42);
}
------------

Output shows that the 'main' function receives a bool message 
type, with value true, but that should only go from subsub to 
blah1?

Main response flags: [Tid(std.concurrency.MessageBox):false, 
Tid(std.concurrency.MessageBox):false]
blah1 Parent tid: 29EFEF8
blah1 Parent tid: 2AEFEF8
Main tid: 413D54
blah1 Hello from this blah1 Tid: 413D54
blah1 Hello from this blah1 Tid: 413D54
subsub GrandParent tid: 2BEFEF4
subsub Parent tid: 2BEFF00
subsub GrandParent tid: 2CEFEF4
subsub Hello from subsub Tid: 413D54
subsub Parent tid: 2CEFF00
Unexpected message: true type bool
subsub Hello from subsub Tid: 413D54
core.exception.AssertError at spawn2(40): Assertion failure
----------------
4205E8
420473
402209
408971
408C0F
40887F
4086F6
402123
40EA60
40EA9A
40E6BB
4270DD
----------------
std.concurrency.OwnerTerminated at std\concurrency.d(248): Owner 
terminated
----------------
4205E8
420473
40D2A4
40D31F
40D027
40CEA2
402327
40A0FE
41F065
428C7C
----------------

I'm guessing Tid can't be safely copied around or something.






More information about the Digitalmars-d-learn mailing list