2

I'm writing a server program in C for academic purpose.

The server lunch a fixed number of worker thread during initialization then the threads start waiting on a "request list".

After that the server starts an infinite cycle where it must do these two things:

  • Check for new connections
  • Check for possible read() on connected clients (only through select/poll not epoll) and remove all readable file descriptors from the set (fd_set in the case we used select() or struct pollfd* in the case we used poll) and put it in the list where the threads are waiting.

In this way worker threads will extract requests from list and serve them. Each request require undefined sequence of read() and write() call.

All I would like to understand if exists a threadsafe way for adding and removing file descriptor from the fd_set or the struct pollfd*.

  • Cannot post this as an answer, but what you need to do is to invoke poll with a short timeout, and periodically check in the main thread whether some changes need to be applied to the `struct polldfd*`. The proposed changes should be put by other threads in some queue for the main thread, and the main thread should pull from that queue in a non blocking manner. – ciamej Jan 21 '19 at 10:29

0 Answers0