`select` on same fd from multiple threads

c, linux, networking

Solution

According to the POSIX 2008 `select` specification, there is nothing that prohibits two threads from both calling `select` at the same time.

It is reasonable to infer that if both threads are monitoring overlapping sets of file descriptors and some of the common file descriptors become readable or writable or have errors diagnosed, then both threads may end up with a report that the common file descriptors are ready. This cannot be guaranteed; there are timing issues to worry about, and it may depend on scheduling of the threads, etc. It also means one of the threads may end up not finding data to read on a file descriptor that it was told contained data to read, precisely because the other thread got there first. Any given byte of data will be read by just one of the threads.

Problem

What will happen if I call `select` on the same open file descriptor from multiple threads? Is this documented somewhere?

Original source