Using objects that manage threads via std.concurrency

monarch_dodra monarchdodra at gmail.com
Tue Feb 12 03:14:56 PST 2013


On Tuesday, 12 February 2013 at 10:08:14 UTC, FG wrote:
> On 2013-02-12 07:58, monarch_dodra wrote:
>> I think I didn't explain myself very well. I have my single 
>> "master" thread
>> which has a "thread-global" mailbox, but I have 3 different 
>> objects that are
>> sharing that mailbox.
>
> OK, I finally get what you are saying.
> You need to create a mailbox and a unique tid for every Manager 
> (and probably would have to change Manager into a class). 
> Unfortunately this won't work out of the box, as for example 
> receiveOnly and friends use only the default mailbox of the 
> current thread.
>
>     struct Manager
>     {
>         Tid tid;
>         MessageBox mbox;
>         this(string s)
>         {
>             this.mbox = new MessageBox
>             tid = Tid(new Mailbox);
>             spawn(&worker, s, tid);
>         }
>         string get()
>         {
>             // you'd have to rewrite receive to use custom mbox
>             return tid.myReceiveOnly!string();
>         }
>     }

Hum, I'll have to try to play around with that. For one thing, 
"MessageBox" is private.

Good news is my manager is already a class.

As for the re-implement of receive to work on a custom Tid, maybe 
it might be better to forget about the tid, and implement it on 
directly on the mailbox? Something like this:

//----
struct Manager
{
     MessageBox mbox;
     this(string s)
     {
         this.mbox = new MessageBox
         Tid managerTid = Tid(new Mailbox);
         spawn(&worker, s, managerTid);
     }
     string get()
     {
         // you'd have to rewrite receive to use custom mbox
         return mbox.receiveOnly!string();
         //Or just straight up:
         mbox.get();
     }
}
//----

I don't know, I'll try and see how it goes.


More information about the Digitalmars-d-learn mailing list