Socket and spawn()

bauss jacobbauss at gmail.com
Sun Jun 2 17:46:09 UTC 2024


On Friday, 31 May 2024 at 16:07:23 UTC, Andy Valencia wrote:
> I'm coding a server which takes TCP connections.  I end up in 
> the main thread with .accept() which hands me a Socket.  I'd 
> like to hand this off to a spawn()'ed thread to do the actual 
> work.
>
>     Aliases to mutable thread-local data not allowed.
>
> Is there some standard way to get something which _isn't_ in 
> TLS?  Or do I have to drop back to file descriptors and do my 
> own socket handling?
>
> TIA,
> Andy

I just want to point out that you should not spawn a thread for 
each accepted socket. That is very bad and expensive.

You should instead make it non-blocking and use something like 
select to handle it.

If anything you should use a thread pool that each handles a set 
of sockets, instead of each thread being a single socket.


More information about the Digitalmars-d-learn mailing list