Is this the best way to share Tids between threads?
TencoDK via Digitalmars-d
digitalmars-d at puremagic.com
Sun Aug 14 03:13:57 PDT 2016
On Sunday, 14 August 2016 at 00:33:18 UTC, Charles Hixson wrote:
> 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?
I'm sending immutable Tid[string] to each thread, though I don't
spawn new threads after app initializing. Works well if you don't
have to encapsulate your threads.
More information about the Digitalmars-d
mailing list