I am confused: is it possible/advisable to combine threads and select on fd?
For example: I want to make a threaded tcp client-server program where the client can sent a message to the server and the server spreads this to all connected clients (and the client can continue to communicate).
I made a server-client program where all the clients get a separate thread when connecting; but i read in beej guide “What if you’re blocking on an accept() call? How are you going to recv() data at the same time? “Use non-blocking sockets!” No way! You don’t want to be a CPU hog. What, then?”
I don’t see what blocking means in this case; because in my program when I enter a text and i push enter the text is send to the server but why does he mean by receiving data at the same time? While I am typing the text? How is that possible? I think I am confused :p
Than I think these are completely different ways of tackling the program but than again i don’t see the advantage the select provides?
Thx
It’s certainly possible to use multiple threads that call
select, but not necessary. A server can serve all clients with just one thread. How? One at a time. Theselectfunction (and its replacements such aspoll,epoll, and so on) can notify you when an event occurs on one of the sockets from the set you supply.selectwill also notify you when alistening socket canaccept. So the server can have just one event loop processing both listening and connected sockets.As for what the advantages and disadvantages are, I’ll just give you a link to The C10K problem article. This is quite an old debate.