Pass Socket to new thread

Ivo ivo at asdasdl.com
Tue Aug 28 10:48:20 UTC 2018


I'm writing a basic server program and I want to handle each 
connection received in a new thread.

So here is the code I'm trying to produce:

while(true) {
     auto client = socket.accept();
     spawn(&handleConnection , client);
}

void handleConnection(Socket client) {
     //do stuff like receive and send
}

I get the error that I cannot pass "client" to "spawn" since it 
is not a "shared" or "immutable" object.
So I need to create an "immutable" (or "shared") copy of the 
socket in order to pass it to the "handleConnection" function. 
However doing so would prevent me from sending/receiving data. 
That is "client.receive" and "client.send" do not work if 
"client" is "shared" or "immutable".

Any suggestions to solve my problem? Thanks.


More information about the Digitalmars-d-learn mailing list