Efficient multithreaded server P&L: -7 (≃ -2025 TWD)
I have some epoll server code that I tweaked to run the server in a thread.
Now I'm taking this code and changing it so that each client connection is multiplexed by multiple threads.
這個想法是每個線程都有一個環形緩衝區,當一個套接字被接受時,一條消息被放在環形緩衝區上,線程開始監聽那個用戶。
所有其他線程都會收到所有用戶的通知,因此他們也可以向他們回顯數據。
這是高效的,因爲一個線程可以服務數以萬計的連接並且有多個線程。
一個問題是多線程的一個通用問題,它是從一個線程爲多種事件中的一種提供服務。我依賴於我正在輪詢 ringbuffer 和 epoll 的事實。
The idea is that each thread has a ringbuffer, when a socket is accepted, a message is put on the ringbuffer and the thread begins listening to that user.
All the other threads are notified of all the users so they can also echo data to them.
This is efficient as a thread can serve thousands-millions of connections and there are multiple threads.
One problem, which is a generic problem for multithreading is servicing one of multiple kinds of events from one thread. I rely on the fact I am polling a ringbuffer and epoll.