www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tid is not a process id?

reply "Enerqi" <kelvin.d.ward googlemail.com> writes:
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 spawn2(40): Assertion failure
----------------
4205E8
420473
402209
408971
408C0F
40887F
4086F6
402123
40EA60
40EA9A
40E6BB
4270DD
----------------
std.concurrency.OwnerTerminated 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.
Jul 24 2012
next sibling parent "Enerqi" <kelvin.d.ward googlemail.com> writes:
Ah no, that example seems to work, confusion.
On Tuesday, 24 July 2012 at 13:48:07 UTC, Enerqi wrote:
 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 spawn2(40): Assertion failure
 ----------------
 4205E8
 420473
 402209
 408971
 408C0F
 40887F
 4086F6
 402123
 40EA60
 40EA9A
 40E6BB
 4270DD
 ----------------
 std.concurrency.OwnerTerminated 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.
Jul 24 2012
prev sibling parent reply "Chris NS" <ibisbasenji gmail.com> writes:
Not sure yet what your exactly issue is, but have you tried to 
reproduce it using register/lookup?
Jul 24 2012
parent "Enerqi" <kelvin.d.ward googlemail.com> writes:
On Wednesday, 25 July 2012 at 02:05:14 UTC, Chris NS wrote:
 Not sure yet what your exactly issue is, but have you tried to 
 reproduce it using register/lookup?
Bad post, that example was OK. The places where I was having errors was from exceptions being lost. Thanks.
Jul 26 2012