Using select and poll on the Same File Descriptor Simultaneously
When multiple threads call select or poll on the same file descriptor (in this case, a socket), the behavior is a bit nuanced. The key point is that both select and poll are designed to be used in a way that allows multiple threads to wait on the same file descriptor.
What Happens When Multiple Threads Wait on the Same File Descriptor?
-
Event Notification: When an event occurs on the file descriptor (e.g., the socket becomes writable because the connection is established), the kernel will notify one of the waiting threads. The notification is not duplicated; it's not the case that both threads will receive the event simultaneously.
-
select and poll Interchangeability: In terms of receiving event notifications, select and poll are essentially interchangeable. The kernel doesn't differentiate between them when it comes to notifying waiting threads. So, if one thread is waiting in select and another in poll on the same file descriptor, when an event occurs, one of these threads will be notified.
-
Which Thread is Notified?: The kernel's decision on which thread to notify is not strictly defined and can depend on various factors, including the scheduling algorithm in use, the order in which the threads called select or poll, and other system-specific considerations. You shouldn't rely on a specific thread being notified; your code should be designed to handle the event notification in any of the waiting threads.
Implications for Your Scenario
In your scenario, where one thread is calling select and another is calling poll on the same socket, when the connection is established:
- Only one thread will be notified that the socket is writable (i.e., the connection is established). It could be either the thread waiting in
select or the one waiting in poll, but not both.
To handle this situation effectively:
-
Design for One Notification: Your application logic should be prepared to handle the connection establishment event in either thread. You might need to use inter-thread communication mechanisms (like condition variables or message queues) to notify other parts of your application that the connection is ready.
-
Avoid Relying on Specific Thread Notification: Don't assume that a specific thread will receive the notification. Make your code flexible enough to handle the event in any thread that's waiting.
-
Consider Using a Single Thread for I/O Multiplexing: If you find that managing multiple threads waiting on the same file descriptor is too complex, you might simplify your design by using a single thread for I/O multiplexing (either with select or poll, but not both on the same FD).
Here's a simple example to illustrate the concept (using poll and select in separate threads):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/select.h>
#include <pthread.h>
int sockfd;
void* poll_thread(void* arg) {
struct pollfd pfd;
pfd.fd = sockfd;
pfd.events = POLLOUT;
printf("Poll thread waiting...\n");
poll(&pfd, 1, -1); // Wait indefinitely
if (pfd.revents & POLLOUT) {
printf("Poll thread: Socket is writable.\n");
}
return NULL;
}
void* select_thread(void* arg) {
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(sockfd, &wfds);
printf("Select thread waiting...\n");
select(sockfd + 1, NULL, &wfds, NULL, NULL); // Wait indefinitely
if (FD_ISSET(sockfd, &wfds)) {
printf("Select thread: Socket is writable.\n");
}
return NULL;
}
int main() {
// Create a non-blocking socket and initiate a connection
// ... (omitted for brevity)
pthread_t poll_tid, select_tid;
pthread_create(&poll_tid, NULL, poll_thread, NULL);
pthread_create(&select_tid, NULL, select_thread, NULL);
// Simulate the connection becoming established after some time
sleep(2); // Just for demonstration; in real code, this would be handled by the connection establishment logic
// Notify the kernel that the connection is established (this is a simplification; actual implementation depends on your connection handling)
// For the sake of this example, let's assume the connection is established and the socket becomes writable
pthread_join(poll_tid, NULL);
pthread_join(select_tid, NULL);
return 0;
}
In this example, only one of the threads (poll_thread or select_thread) will print that the socket is writable. The other thread will remain blocked in its respective system call.
Keep in mind that the actual behavior can depend on the specifics of your system and the implementation details of select and poll on your platform. Always refer to the relevant man pages and documentation for the most accurate information.