std.concurrency.send

Nathan M. Swan nathanmswan at gmail.com
Sat May 19 13:27:18 PDT 2012


On Saturday, 19 May 2012 at 13:26:20 UTC, japplegame wrote:
> Multithreading in D confuses me more and more.
>
> import std.concurrency;
> import std.stdio;
> shared Tid tid;
> void main() {
>   send(cast(Tid)tid, "Hello, World");
> }
> void worker() {
>    writeln(receiveOnly!string);
> }
> shared static this() {
>   tid = cast(shared)spawn(&worker);
> }
>
> I hate these explicit casts. It is impossible sharing anything 
> between threads without these ugly casts from/to shared. Seems 
> like something wrong in program design when I'm forced to use 
> explicit casts. But I don't understand what is it exactly.

You don't need to mark Tids as shared.

> For example. I need create mutable object in one thread and 
> send to another. I don't need to share this object, just 
> create, send and forget. But I have no idea how make this 
> without using shared attribute and casting to/from it.

If you originally create it as shared, you don't need to do the 
casting.


More information about the Digitalmars-d-learn mailing list