Concurrency in D

Justin Whear justin at economicmodeling.com
Wed Jun 27 16:31:28 PDT 2012


On Thu, 28 Jun 2012 00:34:50 +0200, 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). 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? 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.
> 
> 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?
> For all those appoaches, which is the preffered for game programming?
> 
> Thank you.

Concurrency and parallelism are two related, but different concepts. 
Concurrency is when you have different computations conducted 
simultaneously. An example is a modern game engine: you may have a 
rendering thread, a physics simulation, and a networking stack all 
operating at the same time on different cores. Concurrency allows (and 
generally assumes) communication between these parts as they need to 
share information. Message-passing is, as far as I know, pretty much the 
only truly correct way of doing this.

Parallelism is, by contrast, the decomposing of a computation into 
smaller chunks which do not depend on each other. A trivial example is 
incrementing all integers in an array by one--the result of the increment 
computation only relies on the integer being worked on, so the 
computations may be performed in parallel and the results collected.

The std.concurrency and std.parallelism thus exist to solve two different 
problems and are more of an apples-oranges situation.

Justin


More information about the Digitalmars-d-learn mailing list