Is it possible to call accept () on one socket from several threads at the same time?

I am using Linux 3.2.0, x86_64. Can I call accept () on a single socket from multiple threads at the same time?

+5
source share
1 answer

Yes, you can call accept() on the same listening socket from multiple threads and multiple processes, although it may not make as much sense as you think. The core will only allow success. When this is done with processes, this is called preforking, and it saves fork() for every new connection. But when dealing with threads, you can easily have an existing thread pool that is waiting for a queue of new connections. One thread executes accept and writes the queue, and worker threads read the queue and do their work. Itโ€™s cleaner, itโ€™s a well-understood drawing, and you lose almost nothing.

+9
source

Source: https://habr.com/ru/post/949369/


All Articles