Synchronous calls and asynchronous calls

zhaozj2021-02-08  213

Preface: This article has no foundation, just make a summary of the content, hoping to have some use of novice, and welcome everyone to corrections.

The development of the operating system has been very exquisite today, and the thread is a masterpiece. The operating system divides the CPU processing time into a number of short-lived timers, executes a thread instruction in time t1, and performs the instructions of the next thread in turn T2, each thread turns, the result is that all threads are moving in the side of the side. In this way, multiple threads can be created when programming, executed during the same period, and each thread can "parallel" to complete different tasks.

In a single-threaded mode, the computer is a strict von Noymann machine. When a code calls another code, you can only use synchronous calls. You must wait for this code to perform the return result, the caller can Continue to execute down. With multi-threaded support, you can use asynchronous calls, caller, and recipients that can belong to two different threads. After the caller starts the recalled thread, the other party returns the result to continue the subsequent code. After the completion is completed, the caller is notified through some means: the result is already out, please handle it as appropriate.

Some of the computer is more time consuming. When calling this processing code, if the caller is waiting to wait, it will seriously affect the program performance. For example, after a program is started, if you need to open the file to read the data, then according to these data, the program main window will not be displayed, so that the user feels that this program is waiting for half a day, too much . With an asynchronous call, you can easily resolve the problem: put the entire initialization process into a separate thread. After the main thread starts the thread, then go down, so that the main window is displayed instantly. When the user stared at the window, the initialization process was completely completed behind. After the program starts to stabilize, you can continue to use this technique to improve the instantaneous response of human-computer interaction. When the user clicks on the mouse, if the operation is consumed, if the mouse will not be reacted immediately, the entire program is very heavy. With an asynchronous call to the processing fee, let the main thread wait for the next message in any time. If the user clicks the mouse, it is easy and fast, and it will definitely produce a good impression on the software.

Asynchronous calls are used to handle data from external inputs. If the computer needs to request data from a low speed device, then a lengthy data processing process, using synchronous calls obviously very uncomfortable: The computer first issues a request, then wait for data input; and external devices send data to the computer, Also wait for the computer to complete the data processing, then issue the next data request. Both parties have a waiting period, and the entire process is long. In fact, the computer can issue the next data request before processing the data, and then processes the data immediately. If the data processing is fast than the data acquisition, only a computer to wait, the external device can continue to collect data continuously. If the computer simultaneously connects multiple input devices, you can turn data requests to each device and process each device from each device at any time, and the entire system can maintain a continuous high-speed operation. The key to programming is to bring the data request code and the data processing code to two different threads, respectively. The data processing code calls a data request asynchronous function and then trails the data of the hand. After the next set of data arrives, the data processing thread will receive a notification, end the WAIT status, issue the next data request, and then processes the data.

When the asynchronous call is called, the caller is turned around to return the result, so there must be a mechanism to make the caller that can notify the caller when there is a result. There are many means in the same process that can be used, and the means of the author is a callback, Event object, and message.

The callback mode is simple: When the asynchronous function is called, put a function address in the parameter, saves this address, and then calls this function to call the call. If you put an asynchronous function package into an object, you can replace the callback function address with the event, and the event will be notified by the event processing routine. Event is a common synchronization object provided by the Windows system to align the steps between different threads in asynchronous processing. If the caller is temporarily doing, you can call the wait function, etc., at this time, Event is in the nonsignaled status. When the result is called, put the Event object in the signal, the wait function is automatically ended, allows the callback to resume, remove the resulting result from the reciprocity. This approach is complex than the callback mode, the speed is relatively slow, but there is a lot of flexibility, you can make a lot of tricks to accommodate more complex processing systems.

The WINDOWS message is sent is a good choice, both simple and safe. A user message is defined in the program and is prepared by the call party. This message is sent to the caller after being adjusted out of the results and transmits the results through the two parameters of WPARAM and LPARAM. The message is always associated with the window handle, so the caller must receive a message with a window, which is inconvenient. In addition, through message liaison will affect speed, the callback mode is more advantageous when high speed processing is required.

If the two different processes are allocated by the recall, due to the comparative of the memory space, the WINDOWS message sent notification is simple and reliable, and the recall can transmit data to the caller with the message itself. EVENT objects can also be shared between different processes, but can only send data, which cannot transmit data, need to use memory sharing means such as Windows messages and filemaping or communications such as Mailslot and PIPE.

Asynchronous call principles are not complicated, but it is easy to use when practical use, especially when sharing code or sharing data, when you share data, you need to pay attention to whether there is such a sharing when programming, and avoid conflicts through various state signs . The MUTEX objects provided by the Windows system are particularly convenient here. Mutex can only have a jurisdiction at the same time. After a thread gives up the jurisdiction, another thread can take over. When a thread performs to the sensitive area, take over Mutex, so that other threads are blocked by the WAIT function; immediately abandon the jurisdiction immediately, so that the WAIT function is waiting, and the other thread will have the opportunity to visit this sensitive area. This can effectively avoid multiple threads into the same sensitive area.

Since asynchronous calls are prone to problems, you need to design a safe and efficient programming program that requires more design experience, so it is best not to abuse asynchronous calls. After all, the synchronous call is more comfortable: no matter where the program goes, as long as the death is staring at the mobile point, you can have a number in your heart. If it is asynchronous, there is always a four-sided affected, an uneasiness. The asynchronous function can even be converted to a synchronization function if necessary. Method is simple: call the WAIT function immediately after calling asynchronous functions, and then continue to go down after the result is returned.

转载请注明原文地址:https://www.9cbs.com/read-2511.html

New Post(0)