multithreading - simultaneously reading 100 telnet connections c++ -


i want write progam in c++ creates 100+ connections telnet server , read datastreams (parse , interpret them). should use 1 thread every connection? or there method handle many connections without hundreds of threads?

in simplest form, can use 1 thread per connection. won't scale more hundreds of connections, , multithreading may make code , logic more complex (it may make simpler too. depends on application trying do.)

a little better use select. it's function call (all?) socket libraries , operating systems support. basically, put sockets in set , give set select , tell wait event on of these sockets (an event new data arriving on socket or connection error or write completing or stuff that.) if events occur on of these sockets, select call return , tell has happened on socket(s). process events (read incoming data, write more data, handle errors, etc.) , loop , wait more events.

there many tutorials around select , event-driven programming in general. also, there more efficient (albeit platform-specific) system calls , facilities, e.g. poll, epoll, kqueue, inotify, etc.

there of course many excellent libraries use efficient platform-specific method , give (mostly) simple interface work with. libraries such libev, libevent , libuv.

if don't need windows portability, suggest libev. libevent little older , larger, many more features. if need support windows, use libuv.

but handling connections , events part of solution. mentioned comments on question , other answer(s), after receive event on connection, 1 common (not mention sensible , scalable) solution hand off actual processing of data , activities other threads.

what done having pool of worker threads. in main thread, notified of on connection (by select, etc.) instead of doing work in main thread, give work item 1 of worker threads process , generate result , send result back.

one crucial issue here communication between main thread (the select thread) , worker threads. sometimes, form of thread-safe shared queue used. main thread puts works items (events, requests, whatever) in queue, , worker threads try grab item queue whenever not busy.

note read above simplified bare minimum. in real world, writing low-latency , scalable systems of kind challenging , complicated task, might want (a lot) more research if need performance and/or dealing huge amounts of data , many clients.


Comments