A multi-threading model of network programming model __ Programming

Source: Internet
Author: User
Each connection corresponds to a thread

A network socket corresponds to a processing thread, and the socket uses a blocking I/O model;

This model is a common strategy for small programs and Java, and is often used for interactive long connection applications (such as BBS), and also for internal server interaction models. This strategy is difficult to meet the requirements of high-performance programs, the advantage of the implementation is extremely simple, easy to embed complex interactive logic. Apache, FTPD and so on are all this kind of work mode. thread Pool

There are generally two modes of thread pooling: Half-sync/half-async mode and leader/followers mode semi-synchronous, semi-asynchronous mode (Half-sync/half-async)

This model consists of three parts: asynchronous event receiving layer, event synchronization queue, synchronization event processing layer;

Where the asynchronous event sink layer is a thread, the synchronization event processing layer can have multiple threads;

Its workflow is clear:

1. The asynchronous thread is responsible for checking the asynchronous events of the network;

2. When a network event occurs, the asynchronous thread puts the network event into the event queue;

3. The synchronization thread obtains network events from the queue and performs synchronous read or write operations;

The process needs to be careful not to cause two synchronous threads to receive or send a socket at the same time. leader and follower Model (leader/followers)

This pattern is completely different from the Half-sync/half-async, there is no event queue, no fixed event receiver, each thread is the event receiver, but also the processor;

The leader/followers process:

1. Prepare a number of threads to handle a large number of events;

2. There is a thread as leader, waiting for the occurrence of the event, other threads as follower, just sleep;

3. When the event needs to be handled, if leader can be disposed of quickly, leader will again enter the waiting state;

4. If the leader can not be processed immediately, leader from the followers to designate a new leader, to deal with their own events, no longer when leader;

5. Awakened follower as the new leader waits for the occurrence of the event;

6. After processing the thread of the event, it becomes a member of the follower until it is awakened to become a leader;

IOCP is the typical l/f mode of work, and when thread 1 is returned from GetQueuedCompletionStatus, if the process of thread 1 does not exceed a certain time period, it returns quickly. Then continue getqueuedcompletionstatus, the OS will get the new data from the thread 1 getqueuedcompletionstatus, thus reducing the thread's context switch code, conversely, if the thread 1 processing time is longer, Then the new data will be obtained by the getqueuedcompletionstatus of thread 2; multi-process Model

A client corresponding to a process to deal with, but also a long history of the network model, Linux is a typical example of inetd services. This approach is used to deal with intermittent internal data processing, which saves system resources more than the stand-alone mode of its resident memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.