According to the Linux manual page, select is a thread safe function and a cancellation point.
On Linux some operating systems, one thread will successfully enter select, while the other threads would be blocked (the body of select is a critical section). Whatever descriptors are returned to the first thread, then the second thread that successfully enters select would probably wake up immediately with the same set, since select is a level-triggered interface.
Thus, you can't use select to select on multiple sets of file descriptors simultaneously on Linux those operating systems.
Linux seems to support fully re-entrant execution, demonstrated with this test program:
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.
According to the Linux manual page,
select
is a thread safe function and a cancellation point.On
Linuxsome operating systems, one thread will successfully enterselect
, while the other threads would be blocked (the body ofselect
is a critical section). Whatever descriptors are returned to the first thread, then the second thread that successfully entersselect
would probably wake up immediately with the same set, sinceselect
is a level-triggered interface.Thus, you can't use
select
to select on multiple sets of file descriptors simultaneously onLinuxthose operating systems.Linux seems to support fully re-entrant execution, demonstrated with this test program:
When timing this program on Linux (mine was 2.6.43), the program returned after 2 seconds, indicating both threads entered
select
concurrently.According to the POSIX 2008
select
specification, there is nothing that prohibits two threads from both callingselect
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.