Concurrency in D

Adam Burton adz21c at gmail.com
Wed Jun 27 16:05:35 PDT 2012


Minas Mina wrote:

> I have been "playing" latetly with std.concurrency and
> core.thread. I like both, but they are entirely different
> approaches to concurrency.
> 
> std.concurrency: Uses message passing. Does not allow passing of
> mutable types.
> core.thread: Essentially the same as the approach taken by Java.
> 
> 1) Is one favoured over the other? I guess std.concurrency would
> be, because it uses messages (and I recently read somewhere that
> immutability + message passing = good thing).
I've not really got around to doing any concurrent programming in anger with 
D yet but from what I've read message passing is the preferred method. 
Fortunately when it comes to answering your question the concurrency chapter 
for The D Programming Language book (TDPL) was made freely available as a 
preview http://www.informit.com/articles/article.aspx?p=1609144. Hopefully 
that contains your answers :).

> Well, my problem
> about is, how can send mutable data to another thread? _Can_ I do
> it? If not, how am I supposed to share data between them? Not
> everything can be immutable right?
>From what I gather this is where the shared keyword typically comes into 
play which should be detailed in the url above.

> For example I would like to
> have a thread calculate the sum of the first half of an array
> while another thread would calculate the other half, and I could
> add the two results at the end.
For this task there is another concurrency module called std.parallelism 
that is designed for this type of thing (afaik). 
http://dlang.org/phobos/std_parallelism.html

> 
> 2) What about core.thread? To do proper sync between threads in
> core.thread, semaphore, mutexes and stuff like that are needed.
> Are those "good practice" in D?
> 
> 3) I have also read a bit about syncronized classes and shared
> classes/structs, altough I didn't understand it too much (well I
> didn't try too much to be honest).
> 
> For all those appoaches, which is the preffered?
My observation is std.concurrency is being pushed as the preferred default 
method.
> For all those appoaches, which is the preffered for game
> programming?
> 
> Thank you.


More information about the Digitalmars-d-learn mailing list