Is this the best way to share Tids between threads?

Charles Hixson via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 13 17:33:18 PDT 2016


I'm trying to figure out the best way to have multiple threads that can 
each send messages to the other threads.

I've come up with two basic approaches, one uses the main thread as a 
relay, and the other uses a shared array of Tid...thus:

import    std.concurrency;
import    std.stdio;

import    core.thread;

shared    Tid[8]    tidList;

void    worker (int ndx)
{ Thread.sleep (5.msecs);
     writeln ("worker is done");
}

void    main()
{
     auto tid    =    spawn (&worker, 0);
     tidList[0]    =    cast(shared Tid)tid;
     Thread.sleep (40.msecs);
     writeln ("main is done");
}

Is there a reasonable way to decide which way is best?  Is there a 
better way that I just haven't thought of? Are there problems with 
sending messages via shared Tids that I just haven't thought of yet?



More information about the Digitalmars-d mailing list