Threads & Message Passing - Best practices ?

Jonas Mminnberg via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 11 02:56:01 PDT 2017


I am using the recommended message passing threading model for my 
project, and it is not always clear how to do things in the best 
way;

1. receive() pattern matching -- what is common here, always send 
(int, someData) where int is a value you compare to. Or create 
dummy types that you can match on directly?

   receive(int cmd, int[] data) { if(cmd == SomeCommand) 
doStuff(data); }
   receive(SomeCommand, int[] data) { doStuff(data); }

The latter seems nicer, but what's the best way to define those 
types?

Or maybe it's better to be even more general?  Something like;

receive(void delegate(ARGS) f, ARGS a) { f(a); }


2. What is the reason you can't decide which thread(s) to 
receive() from and is there any way around it? Right now I need 
to have a static updateAll() method that is aware of all 
instances and call update() on them as needed. Also it's very 
hard to have generic modules that has their own threads because 
they can receive messages from other modules threads...

3. Is there a recommended way to deal with creating immutable 
data ? Right now I just cast to immutable before returning the 
data since I know I have the only reference, but it would be nice 
to have something like a `UniqueReference` containing a 
immutableMove() method...




More information about the Digitalmars-d mailing list