std.concurrency thread communication problem

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 17 12:33:53 PDT 2014


On Saturday, 17 May 2014 at 18:43:25 UTC, Charles Hixson via 
Digitalmars-d-learn wrote:
> I'm building a program which I intend to have many threads that 
> can each send
> messages to (and receive messages from) each other.  The 
> obvious way to do
> this would be to have a shared array of Tids, but this seems to 
> not work.  I'm
> continually fighting the system to get it to compile, and this 
> makes me think
> it should probably be done some other way...but what?
>
> One possibility is to have each thread maintain a separate 
> array that contains
> all the threads, which would mean that they would need to be 
> initialized after
> they were created.  This would avoid the problems of shared 
> Tids, but each Tid
> contains a private mailbox, so this would be being duplicated, 
> and that
> bothers me...it seems like a poor idea.  (Maybe I'm wrong about 
> that...but I
> don't know.)

If my understanding is correct, each Tid contains a reference to 
the corresponding thread's MessageBox (implemented by way of 
MessageBox being a class), not an independent instance. You 
should be fine to just have an array of the relevant Tids in each 
thread.

Alternatively, a single __gshared array of threads should work, 
given you are sufficiently careful with it. Remember, if no-one 
is doing any writing then you don't need to do any 
synchronisation of reads.


More information about the Digitalmars-d-learn mailing list