Best choice for messages (std.concurrency)

Jonathan M Davis jmdavisprog at gmail.com
Wed Aug 18 14:14:13 PDT 2010


On Wednesday, August 18, 2010 13:48:08 nsf wrote:
> Hi. I'm trying to replicate in D my Go demo app which basically
> draws mandelbrot fractal using multiple goroutines. In Go it's
> fairly easy to communicate between threads, because the memory model
> is C like. D 2.0 on the other hand has this thing "TLS by default".
> And I saw simple examples of std.concurrency usage in Andrei's book,
> but what about more complex ones.
> 
> I have a need to pass some kind of a message to a worker thread.
> Let's call it Request. It should contain request data, flag that is
> used to interrupt thread if the response isn't needed anymore and
> obviously the sender ID. What's the best way to do that?
> 
> Something like:
> shared struct Request {
>     bool discard;
> immutable:
>     byte[] request_data;
>     Tid sender;
> }
> 
> ? Or maybe I don't understand something. How this struct should be
> allocated in order to be able to pass it between threads? Or should
> I use class instead? I think it's very confusing. Any suggestions?

Well, if you have the book, I would have thought that it explained it well 
enough. You use send() and receive() in std.concurrency to send messages back 
and forth. All communication is done through them. The data passed across is 
either a value type which is copied, or it's immutable. There is no sharing 
across threads. If it's a struct, you should be able to just pass it across.

Now, the fact that you're trying to pass a _shared_ struct is just weird. So 
maybe that's your problem. That isn't going to work. Something which is shared 
is no longer thread local, at which point you need to worry about 
synchronization and all that. You pass normal structs across. They data is 
copied. If you want to send a message back, then send a message back using 
send(). Trying to use a shared flag in something that you pass across just 
doesn't make sense.

If you really need to, you can use shared data, and you can have both shared 
data and message passing in the same program, but the ideal is to just use 
message passing, and with a flag, that should be quite straightforward. You just 
send a message indicating whatever the flag is meant to indicate or just straight 
up send the flag across. Now, that may mean altering how your program works, but 
the idea is to constrain all communication between threads to the data sent via 
send() and have no data sharing beyond that.

I'd really suggest reading the concurrency section of the book again. I had 
thought that it was quite clear on the matter.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list