On Wednesday, 4 March 2020 at 23:37:00 UTC, Martin Brezel wrote:
The documentation for spawn() states: "all arguments to fn must either be shared or immutable or have no pointer indirection."

The easiest thing to do is to just cast

import std.concurrency;
import std.socket;

void main() {
        auto s = new Socket();
        spawn((shared Socket s_) {
            Socket s = cast() s_;
        }, cast(shared) s);
}


Note the shared argument to the function... then cast AWAY shared inside that function, and cast TO shared when passing the argument.

That basically just tricks the compiler. But if you aren't going to actually use the socket from the original thread anymore, it should be perfectly fine to do.

(if you do actually share it across threads, using it from both... it would actually probably still be fine - the Socket class is a thin wrapper around OS functions that work with threads. But then you re on your own, the compiler won't help get it right at all. Abandoning the old one isn't really getting compiler help either but it is so easy to do that you prolly won't make a mistake.)


You can also not use `spawn` and instead give `new Thread` a try from core.thread. That's totally DIY, the compiler won't even try to help you, but it can be simpler if you know what you're doing and don't need to pass a lot of

Reply via email to