Pascal de Kloe
Pascal de Kloe

Reputation: 550

Read multiple sockets from single thread [low-latency]

As a network client I would like to follow the input from two TCP connections. Both servers send a few packets per second. Each packet is small in size when compared to the available bandwidth.

Ideally we'd like to read both sockets from a single thread for the best latency. My initial plan was to use FIONREAD from ioctl(2) on each connection in a loop, and only read when data is available. Are there other options? CPU consumption is less relevant than low latency.

Upvotes: 0

Views: 126

Answers (1)

user9706
user9706

Reputation:

You want to use one of these I/O multiplexing primitives:

  • Non-blocking sockets which would give you similar behavior as your ioctl(2). Usually, it's combined with one of the options below to avoid a busy loop.
  • select(); "On Linux [...] it may be safer to use O_NONBLOCK on sockets that should not block"
  • poll()
  • epoll() (Linux)
  • POSIX aio_ family of functions. Not sure it's used much and Linux the library spawns threads.
  • uring (Linux)

Upvotes: 1

Related Questions