Using objects that manage threads via std.concurrency
monarch_dodra
monarchdodra at gmail.com
Mon Feb 11 22:58:04 PST 2013
On Tuesday, 12 February 2013 at 06:29:22 UTC, Ali Çehreli wrote:
> On 02/11/2013 01:37 PM, monarch_dodra wrote:
>
> > What should a manager do if it
> > calls "receive", and notices the message wasn't meant for him?
>
> Threads receive their own messages. If there is a specific
> receiver of a message, then the child sends it to that
> receiver. As FG said, every thread has a separate mailbox.
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.
Code example:
//----
import std.stdio, std.concurrency;
struct Manager
{
this(string s)
{
spawn(&worker, s, thisTid);
}
string get()
{
return receiveOnly!string();
}
}
void worker(string s, Tid owner)
{
owner.send(s);
}
void main()
{
auto ma = Manager("a");
auto mb = Manager("b");
auto mc = Manager("c");
writeln(ma.get());
writeln(mb.get());
writeln(mb.get());
}
//----
How can I get my 3 managers to co-exist, when they are all
sharing the same box? How can I make sure the workers are sending
their messages to the correct manager?
> It is possible to introduce threads to each other by their
> thread ids, which can be mapped to arbitrary names. (See
> register, locate, and unregister in std.concurrency.)
Yes, but in this case, the problem is not thread to thread
communication, but rather thread to object
> > Does any one have any (simple) literature on the subject?
>
> My experiments have been documented in the following chapter:
>
> http://ddili.org/ders/d.en/concurrency.html
>
> The "Thread names" section in there has a simple example that
> involves a third party introducing two threads to each other.
>
> Ali
I have already fully read that tutorial, which was very helpful.
Thank you.
More information about the Digitalmars-d-learn
mailing list