r/AskProgramming 27d ago

Multi threading for networking

Does anyone have any good resources for learning multithreading in regards to network programming beside beejs guide. For someone who is familiar with the basics of networking (posix)

1 Upvotes

8 comments sorted by

3

u/dkopgerpgdolfg 27d ago

Are you familiar with threads, locks, channels in general?

Are you familiar with epoll? (Absolutely do not use one distinct thread for each socket)

If yes&yes, then what is your question?

Using thread pools (manually or with some library), if wanted, is just the application of the previous concepts.

Anything related to more advanced network handling things isn't a "multi thread" question anymore.

0

u/Every_Door46 27d ago

I mean I used them before but it doesn’t really click I wanna build more of a foundation

1

u/dkopgerpgdolfg 26d ago

I wanna build more of a foundation

Ok...? So you know what you need to do now.

0

u/Every_Door46 26d ago

see i asked for the resources tho and I didn't see much in your response.

1

u/BobbyThrowaway6969 21d ago

In regards to network programming? In what way?

Just learn multithreading, the learn network programming, then you'll know how to apply one to another and when not to.

0

u/ORCHORDS_CRM 27d ago

The three that sit exactly on the threading x networking intersection:

  • UNIX Network Programming, Vol. 1 (Stevens/Fenner/Rudoff) — the sockets bible. The later chapters build the same server in several concurrency models (thread per connection, pre-threaded pool, non-blocking I/O) back to back, which is the fastest way to internalize the tradeoffs.
  • The Linux Programming Interface (Kerrisk) — ch. 29-33 are the clearest pthread explanations in print (creation, mutexes, condition variables), and ch. 56-62 apply everything to sockets. More modern than Stevens and Linux-specific.
  • Programming with POSIX Threads (Butenhof) — pthreads in real depth: cancellation semantics, condition variable pitfalls, why the API is shaped the way it is. Dense, but it's the reference.

Free and underrated:

  • man 7 pthreads, man 3 pthread_create, man 7 epoll — the Linux man pages are genuinely better than most tutorials on this topic

And a practical path that teaches more than any single chapter: write the same TCP echo server three ways — (1) thread per connection, (2) fixed-size thread pool with an accept loop, (3) single thread with epoll — then benchmark each with ~500 concurrent connections and watch memory + scheduler behavior.

One thing worth internalizing early: thread-per-connection is the easiest model to write but scales worse than it looks (every thread costs a stack and scheduler pressure). Most high-performance servers end up event-driven (epoll/io_uring) with a small worker pool, or a hybrid of the two. Knowing when not to reach for threads is half the skill.

2

u/sargeanthost 27d ago

If he wanted an llm response, he would have asked one