I have a simple fcgi server written in c++ - for now it's single threaded. I saw the main loop for example like
accept_connections();
handle_data();`
handle_connections();
but what if I want to respond to each client simultaneously? Because if I understand this, server first accepts new connections and iterate over them and proccess each. But this can be not good because of for example a upload (while one client uploads, others have to wait.) or by DoS like slowloris. How can server do multiple clients at one time ? I saw one server it was single-threaded - and did it.
How to do it ?
I solved it earlier but i want to answer my question.
Solution I made:
I wrote custom fastcgi handler. I also used libev for network threads, buffered and direct output and automatic waking and disabling write event. Debugging with valgrind. Compiler clang.
Reactions to other answers:
I didnt want to use thread per connection because it has some cost in spawning and also if there are lots of threads, there is also a slowdown and higher memory usage.
And I didnt use boost asio because I dislike frameworks and asynchronous IO was not one that i was looking for.
使用单个线程查看Boost.Asio以获取异步I / O.
You could spawn a new thread for each incoming connection. To increase efficiency, use a thread pool to avoid the overhead of creating a new thread all the time.
There are alternative/better methods, but this one is quite simple and you can continue to use blocking sockets. Watch out for synchronization issues when accessing shared resources from your threads though!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.