When two threads in the same process use select and poll concurrently to monitor the same socket, both threads will receive the "connected" event (or other readiness events) if the socket becomes ready. Here's a detailed explanation:
Key Points:
-
Shared File Descriptor State:
- The socket's readiness (e.g., "connected," "writable," or "readable") is a process-wide state tracked by the kernel. Both
select and poll observe this shared state.
- If the socket transitions to a ready state (e.g., after a successful non-blocking
connect()), both threads will detect this state change when they check the socket.
-
System Call Behavior:
select: When called, it checks the socket's current readiness and waits for changes.
poll: Similarly, it checks the socket's readiness and waits for events.
- If the socket is already ready when
select or poll is called, they return immediately. If not, they block until the socket becomes ready (or a timeout occurs).
-
Concurrency Considerations:
- If both threads are blocked in
select and poll when the socket becomes ready, the kernel will wake both threads. Both will return from their respective system calls with the event.
- If one thread handles the event (e.g., reads/writes the socket) before the other thread checks, the second thread may see a different state (e.g., data available for reading instead of just "connected").
Example Scenario:
Suppose:
- Thread A is blocked in
select waiting for the socket to become writable (indicating connect() completed).
- Thread B is blocked in
poll waiting for the same socket to become writable.
When the socket becomes ready (e.g., connect() completes):
- Both threads will unblock and detect the event:
select will set the socket's file descriptor in writefds.
poll will set POLLOUT in revents.
Race Conditions:
While both threads will detect the event, race conditions may occur in how they handle the event:
- Thread A might read/write the socket first, consuming data or changing its state.
- Thread B might then see a different state (e.g., no data remaining to read).
- To avoid this, ensure proper synchronization (e.g., locks) if multiple threads act on the socket.
Code Example:
Here's a simplified example of two threads using select and poll on the same non-blocking socket:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <poll.h>
#include <pthread.h>
int sockfd; // Shared socket between threads
void* thread_select(void*) {
fd_set writefds;
FD_ZERO(&writefds);
FD_SET(sockfd, &writefds);
printf("Thread SELECT: Waiting...\n");
select(sockfd + 1, NULL, &writefds, NULL, NULL);
printf("Thread SELECT: Connection ready!\n");
return NULL;
}
void* thread_poll(void*) {
struct pollfd pfd = {sockfd, POLLOUT, 0};
printf("Thread POLL: Waiting...\n");
poll(&pfd, 1, -1); // Infinite timeout
printf("Thread POLL: Connection ready!\n");
return NULL;
}
int main() {
// Create a non-blocking socket (similar to previous example)
sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
// ... (connect() in non-blocking mode, etc.)
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_select, NULL);
pthread_create(&t2, NULL, thread_poll, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
Output:
Thread SELECT: Waiting...
Thread POLL: Waiting...
Connection established (via poll). // (Assuming the socket is ready)
Thread SELECT: Connection ready!
Thread POLL: Connection ready!
Key Takeaways:
- Both
select and poll will detect the socket's readiness because they monitor the same file descriptor.
- Race conditions can occur if threads act on the socket without synchronization.
- Use synchronization primitives (e.g., mutexes) if multiple threads need to handle the socket's state changes.
In summary, the kernel ensures that both threads will be notified of the socket's readiness, but care must be taken to avoid conflicting actions in concurrent code.