The thread structure is equivalent to the soul of the server program, and a good server program must have a thread structure and high thread utilization. The following mainly in the form of pseudo-code to enumerate some common thread structure.
1 single Business processing threading structure
int main () {Init (); while (queue. GetMessage (timeout, message))//the queue here to support multiple thread write, a thread read {dispatchmessage (message);D Etecttimer ();//If a timer is required}deinit (); return 0;}
The Windows window program and most of the online games I have seen use this thread structure. Because there is only one business processing main thread, there is no need to consider thread synchronization when doing business processing. Of course, this server is not just a single thread at work, usually the network and db (if any) use separate threads, such as when the network receives a message, enqueue into the queue, then queue. GetMessage returns, the corresponding processing function is found by DispatchMessage to do the specific processing.
2 Multi-Service processing threads
int main () {Init (); for (1 to ten) {Startthread (&threadfunction);} WaitForExit ();D einit (); return 0;} int Threadfunction () {while (queue. GetMessage (timeout, message))//queue to support multi-write multi-read {dispatchmessage (message);} return 0;}
After the network thread receives the message, Enqueue to the queue, where the getmessage of one thread is returned and processed. The DB module typically uses this threading structure.
3 Concurrent Single Thread
Class Workthread{public:void Start ();p rivate:void asnycread (int clientId), void asnycwrite (int clientId, message* message), void OnRead (int clientId, message* Message), void onwritecomplite (int clientId);}; int main () {for (1 to ten) {new Workthread ();} WaitForExit (); return 0;}
I actually did not find a good name and pseudo-code to describe this thread structure, add a bit of text to explain it. This thread structure typically turns on multiple threads at program startup (usually CPU cores), each thread fully loads its desired configuration, each thread is independent and functionally identical, there is no interaction between threads and threads, and there is no queue in user code. Once a connection is established, it is always running in the same thread space, so there is no thread synchronization. This kind of line program structure is very suitable for the Gateway class, the forwarding class and so on slow operation less server, can make full use of multi-core CPU resources, improve service efficiency. This threading structure is easily implemented using IOCP and Boost.asio.
4 thread pool
int main () {Init (); for (1 to) {Threadpool.add (New Workthread ());} Waittask (); WaitForExit ();D einit (); return 0;} void Waittask () {Workthread t = threadpool.getfree (); T.wakeup ();//wake-up of the child thread immediately after the child thread waits for the client to connect, and then Wakeup the next thread to wait for the client connection after the connection succeeds. Task performed by this thread}
Once compared to the leader/follower mode of fire, it is said that Tomcat is using this, but the personal feel that this structure is not very efficient, because this mode of network IO is usually synchronized, greatly reducing the utilization of threads.
Conclusion
Above only a simplified description of the threading structure, the actual use of the server is not so simple, it may be a combination of multiple structures, it may be the above mentioned thread structure. But the high-concurrency-efficient server essentials are simple: increase the utilization of each thread, avoid busy waiting, sleep, and excessive thread switching, while reducing lock-in and queue-in list columns. Business processing can be refined, from a single server processing to multi-server processing, can be concurrent concurrency ... However, this is beyond the scope of this article and has the opportunity to elaborate.
Attached Testimonials
Before 08, boost was not so popular, I usually use my own implementation of thread-related base libraries, such as mutexes, Condition, Thread, Sharedptr, messagequeue--of course, is basically copied from the boost. Time Gallop, 2016 now, C11 also contains most of the above library, and the remaining MessageQueue realized is also minutes of things ... The times are even better!
Thread Structure of server development