Cerl2 Series 2: Should I use synchronous or Asynchronous Network Programming?

Source: Internet
Author: User

In C/C ++, both Network Libraries libevent and boost ASIO adopt asynchronous programming models. When an IO completion event occurs, a callback function is called to process it. This programming model has a good Io throughput. However, the cost is huge:

  1. Ugly code. The application logic is broken down by callback functions.
  2. Complex memory management. When a callback function is executed, the corresponding memory is released.
  3. Debugging is difficult. Because the function is cut, the code execution sequence cannot maintain the consistency of thinking during debugging.

The Erlang language solves this problem well. It introduced the "Lightweight Process" solution. The concept of this solution is very simple: a single process uses synchronous calls, and enough processes are used to make Io parallelization and improve Io throughput.

 

In fact, similar ideas are not first put forward by Erlang. Let's look at the operating system. A single process (or thread) adopts synchronous io (read/write ). Do I have to perform multiple I/O operations at the same time? Create a new process (or thread) to do this.

 

Therefore, Erlang's real innovation lies in the "lightweight" of "processes ". Only when the process is lightweight can you create a process with no scrubs. Io parallelization can be implemented.

 

Therefore, the most fundamental point I admire the Erlang language is that it tells everyone that the code of high-performance network servers can be very simple and elegant.

 

Cerl2 inherits the Erlang or operating system philosophy. For example:

 

Suppose we have implemented an RPC code for a single server:

 

Void call (Result & result, const host & host, const request & req );

 

This is to send a req request to the host and return result.

 

If we want to send the same request to multiple servers at the same time, this is called multi_call in network programming. The prototype is as follows:

 

Void multi_call (result results [], const host hosts [], size_t N, const request & req );

 

To implement this function in cerl2, you do not need to modify the call code. Simply use N fiber threads to make a separate call.

 

Therefore, cerl2 programming is very simple, because you have been thinking in this way for a long time, aren't you?

 

Finally, let's repeat the most important principles in cerl2 programming philosophy:

In cerl2, only synchronous calls are allowed. To make Io parallel, use multiple fiber threads (fiber.

 

Related Article

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.