About the things that are synchronous and asynchronous

Source: Internet
Author: User

Synchronous and asynchronous are two very important concepts, which are widely used, such as synchronous and asynchronous process, synchronous transmission and asynchronous transmission, etc. The two concepts are always vague and confusing, and it is hard to say for a moment. So we collected some information, and did the relevant collation, in-depth analysis of synchronization and asynchronous mechanism.

Process synchronization vs. asynchronous

Process synchronization: When a function call is made, the call cannot be returned until the result is obtained. One sentence of von Neumann can describe the synchronization of a process-"the program is put into memory and executed sequentially". That is, synchronization must guarantee the order in which events are executed, and the preceding events are the basis for the execution of subsequent events, which must wait for the previous event to be executed. The synchronization relationship between processes refers to inter-process interdependence, such as the relationship between consumers and production. In practice, examples such as SendMessage, which send a message to a window, do not return until the other party finishes processing the message. When the other party finishes processing, the function returns the LRESULT value returned by the message handler function to the caller.

SendMessage (...)  TRACE0 ("just  like  send");  

SendMessage is not returned when the call is called, and the TRACE0 is executed after the message responds.

The concept of asynchrony is relative to synchronization, and when an asynchronous procedure call is sent, the caller cannot get the result immediately. The part that actually handles this call is notified to the caller by state, notification, and callback, after completion. Example of such an example: Xiao Ming play mobile phone. As shown:

Figure 1

When the phone is charging, you can choose to wait, but waiting is boring, so you can watch TV to pass the time. When your phone is fully charged, you can continue to play your phone. In this, there is the synchronous and asynchronous process. Synchronization is, the phone is not power can call the charging function to charge the phone, you must wait until the phone is full before you can continue to play mobile phone. That is to say, when the charging is over to continue to play the phone. Asynchronous is, after calling the charge function, you can not wait for the function to complete, but to watch TV. When the charging function is complete, you can use the status, notification, and callback methods to get the result of the call.

And the SendMessage corresponds to the postmessage.

PostMessage (...)  TRACE0 ("just  like  WSASend  using  overlapped");  

PostMessage is returned immediately after the call and executes TRACE0 without a message response.

In fact, synchronization is the calling module waiting for a callee to return, before proceeding to the next step.

Instead of waiting for the call to return, you can proceed to the next step after the call is made by the calling module.

Synchronous transfer and asynchronous transmission

In the network communication, the communication between the two sides to exchange data, need a high degree of collaboration. In order to interpret the signal correctly, the receiver must know exactly when the signal should be received and processed, so timing is critical. In the computer network, the timing factor becomes the bit synchronization. Synchronization is to receive data from the beginning and end of each bit sent by the sender, or the error will occur. A bit can usually be synchronized synchronously or asynchronously.

Asynchronous transfer (asynchronous transmission): The asynchronous transfer divides the bitstream into groups for transmission, which can be 8-bit 1 characters or longer. The sender can send these bit groups at any time, and the receiver does not know when they will arrive. The keyboard can send code at any time, depending on the user's input speed, and the internal hardware must be able to receive a typed character at any moment.

There is a potential problem with asynchronous transmissions, where the receiver does not know when the data will arrive. The first bit has passed before it detects and responds to the data. It's like someone coming up from behind unexpectedly and talking to you, and you missed out on the first few words before you could react. Therefore, each asynchronous transfer of information begins with a starting bit, which notifies the receiver that the data has arrived, giving the receiver the time to respond, receive, and cache the data bits, and at the end of the transfer, a stop bit indicates the termination of the transmission information. By convention, a line that is idle (without transmitting data) actually carries a signal that represents a binary 1, and the starting bit of the asynchronous transmission makes the signal 0, and the other bits make the signal change with the data being transmitted. Finally, the stop bit causes the signal to change back to 1, and the signal remains at the next start bit. For example, on the keyboard the number "1", according to the 8 bit of extended ASCII encoding, will be sent "00110001", at the same time need to precede the 8 bit with a starting bit, the back of a stop bit.

Asynchronous transmission is easy to implement, because each information is added to the "synchronization" information, so the timing of the drift will not generate large accumulation, but it has more overhead. In the example above, two bits are transmitted per 8 bits, and the total transmission load increases by 25%. For low-speed devices with very little data transmission, the problem is small, but for high-speed devices with large data transfers, a 25% increase in load value is quite serious. Therefore, asynchronous transmissions are often used in low-speed devices.

Synchronous transfer (synchronous transmission): The bit groupings for synchronous transfers are much larger. Instead of sending each character independently, each character has its own start and stop bits, but it combines them together to send. We call these combinations a data frame, or simply a frame.

The first part of the data frame contains a set of synchronized characters, which is a unique bit combination, similar to the starting bit mentioned earlier, to inform the receiver that a frame has arrived, but it also ensures that the receiver's sampling speed and the speed of the bit are consistent, so that the sending and receiving parties enter the synchronization.
The last part of the frame is a frame end tag. Like a synchronous character, it is also a unique bit string, similar to the stop bit mentioned earlier, to indicate that there is no other data to arrive at the beginning of the next frame.

Synchronous transmissions are often much faster than asynchronous transmissions. The receiver does not have to start and stop the operation for each character. Once a frame synchronization character is detected, it receives them when the next data arrives. In addition, the overhead of synchronous transmission is relatively low. For example, a typical frame might have 500 bytes (that is, 4000 bits) of data, which might contain only 100 bits of overhead. At this point, the increased bit bit increases the total number of bits transmitted by 2.5%, which is much smaller than the 25 increment in asynchronous transmissions. As the actual bit of data in the data frame increases, the percentage of overhead bits will be reduced accordingly. However, the longer the data bit, the greater the buffer required to cache the data, which limits the size of one frame. In addition, the larger the frame, the longer it occupies the continuous time of the transmission media. In extreme cases, this will cause other users to wait too long.
Synchronous transmission mode in the sender and receiver of the clock is uniform, the transmission between characters and characters is not synchronous interval.
Asynchronous transmission does not require the sender and receiver of the exact same clock, the transmission between characters and characters is asynchronous.

The difference between synchronous and asynchronous transmissions

    1. Asynchronous transmission is a character-oriented transmission, while synchronous transmission is a bit-oriented transmission.
    2. The units of the asynchronous transmission are characters and the units that are transferred synchronously are frames.
    3. Asynchronous transmission through the beginning and end of the character start and stop code to seize the chance of resynchronization, while the synchronous transmission is to extract synchronization information in the data.
    4. Asynchronous transmissions have lower requirements for timing, and synchronous transmissions often coordinate timing through specific clock lines.
    5. Asynchronous transmissions are less efficient than synchronous transmissions.

Blocking and non-blocking

A blocking call means that the current thread is suspended until the call results are returned. Functions are returned only after the result is obtained. Someone might equate blocking calls with synchronous calls, and in fact he is different. For synchronous calls, many times the current thread is still active, but logically the current function does not return. For example, we call the receive function in CSocket, and if there is no data in the buffer, the function waits until there is data to return. At this point, the current thread will continue to process a wide variety of messages. If the main window and the calling function are in the same thread, the main interface should be refreshed unless you call in a special interface action function. Another function that the socket receives data recv is an example of a blocking call. When the socket is working in blocking mode, if the function is called without data, the current thread is suspended until there is data.

The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.
Blocking mode and blocking function calls for objects
Whether the object is in blocking mode and if the function is not a blocking call has a strong correlation, but not one by one corresponds. Blocking objects can have non-blocking calls, we can use a certain API to poll the state, the appropriate time to call the blocking function, you can avoid blocking. For non-blocking objects, calling a special function can also enter a blocking call. The function Select is an example of this.

About the things that are synchronous and asynchronous

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.