spawn a function with object as arg?

Martin Brezel martin.brzenska at googlemail.com
Wed Mar 4 23:37:00 UTC 2020


I want to create a Client, which receives and handles received 
data in  background while the Client can send via the same socket.

My first idea was something like this:
>class Client {
>	private Socket socket;
>	private Tid receiverTid;
>
>	this(Socket socket) {
>		this.socket = socket;
>		this.receiverTid = spawn( &receiver, this.socket );
>	}
>	
>	void command(Command[] cmd) {
>		this.socket.send(cmd.makeMessage());
>	}
>}
>
>void receiver(Socket socket) {
>	/*..*/
>}
..how naive of me :)

The compiler says: "Error: static assert:  "Aliases to mutable 
thread-local data not allowed."
The documentation for spawn() states: "all arguments to fn must 
either be shared or immutable or have no pointer indirection."

So, now i am thinking about to create a instance of Socket inside 
the spawned worker and do send and receive via Message Passing 
between Client and the worker.

But, is there another way?
I am writing lot of golang lately and i am sure something like 
`go receive(&socket)` would be valid. Now, i am wondering if 
there is a similar simple way to do it in dlang.




More information about the Digitalmars-d-learn mailing list