Asynchronous execution method and device of instructions and electronic equipment

By receiving and parsing the target function data of the main thread in the target thread and using anonymous functions such as Lambda functions for asynchronous execution, the problems of complex and high cost of code migration in the existing technology are solved, and efficient code migration and stability are achieved.

CN114995968BActive Publication Date: 2025-10-21NETEASE (HANGZHOU) NETWORK CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210430231.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-22
Publication Date
2025-10-21
Estimated Expiration
2042-04-22

AI Technical Summary

Technical Problem

In the prior art, migrating program code from the main thread to other threads for execution requires complex code rewriting and high labor costs, which leads to low efficiency, especially when the code coupling is high.

Method used

By receiving the basic data of the target function sent by the main thread in the target thread, parsing and determining the calling parameters, and using anonymous functions such as Lambda functions for asynchronous execution, the porting process of non-anonymous functions is avoided, and Lambda functions and FIFO queues in the C++ language are used for data transmission.

Benefits of technology

It improves code writing efficiency and stability, reduces labor costs, and enables simple and efficient code migration.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114995968B_ABST
    Figure CN114995968B_ABST
Patent Text Reader

Abstract

The application provides an asynchronous execution method and device of instructions and electronic equipment, in response to a calling trigger event for a target thread, starting the target thread; receiving basic data of a target function corresponding to to-be-executed instructions sent by a main thread through the target thread; determining calling parameters of the target function based on the basic data of the target function through the target thread; wherein the calling parameters of the target function include data parameters and type parameters of the target function; controlling the target thread to call the target function based on the calling parameters, and executing the to-be-executed instructions through the target function. This way executes the to-be-executed instructions through the target thread calling the anonymous function corresponding to the to-be-executed instructions, without transplanting the code corresponding to the to-be-executed instructions through a non-anonymous function, improving the code writing efficiency and stability, and reducing the labor cost.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method, device and electronic device for asynchronously executing instructions. Background Art

[0002] When the Application Programming Interface (API) used by part of the program code is only allowed to be executed on a certain thread, or based on the execution efficiency of the code, part of the program code needs to be handed over to other threads for execution, this part of the code needs to be rewritten. In the related art, the code snippet is usually transplanted into a non-anonymous function. During the transplantation process, it is necessary to consider the degree of coupling between the code snippet and other codes, flexibly set the variables in the non-anonymous function, and set the trigger variable of the function. Another thread judges whether the variable value of the trigger variable is true in the loop. If it is true, the non-anonymous function is called. This method is more complicated for the transplantation process of program code that needs to be called by other threads for execution, has low efficiency, and high labor costs. Summary of the Invention

[0003] In view of this, an object of the present invention is to provide a method, apparatus and electronic device for asynchronous execution of instructions, so as to improve the efficiency of porting program codes that need to call other threads for execution and reduce labor costs.

[0004] In the first aspect, an embodiment of the present invention provides an asynchronous execution method of instructions, the method comprising: responding to a call trigger event for a target thread, starting the target thread; receiving basic data of a target function corresponding to the instruction to be executed sent by a main thread through the target thread; based on the basic data of the target function, determining the calling parameters of the target function through the target thread; wherein the calling parameters of the target function include data parameters and type parameters of the target function; controlling the target thread to call the target function based on the calling parameters, and executing the instruction to be executed through the target function.

[0005] The basic data of the above-mentioned target function includes the data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function and is used to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; based on the basic data of the target function, the step of determining the calling parameters of the target function through the target thread includes: parsing the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; controlling the target thread to call the corresponding calling function through the storage address of the function type, and determining the type parameters of the target function through the calling function.

[0006] The above-mentioned step of parsing the basic data of the target function to obtain the storage address of the function type of the calling function includes: extracting the storage address of the calling function type from the basic data based on the storage parameters of the storage address of the function type of the calling function through a preset memory capacity measurement function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage position in the basic data.

[0007] The above method is applied to the C++ language environment; the step of determining the type parameter of the target function by calling the function includes: forcibly converting the data parameter of the target function into corresponding character pointer data by calling the function, and forcibly converting the type of the character pointer data into the type parameter of the target function.

[0008] The step of receiving the preset basic data of the target function sent by the main thread through the target thread includes: storing the basic data of the target function into a message queue preset in the target thread.

[0009] The basic data of the above-mentioned objective function is generated in the following manner: obtaining the data parameters of the objective function and the calling function corresponding to the objective function; constructing the calling function based on the data parameters of the objective function to determine the type parameters of the objective function; generating initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data; generating basic data based on the initial basic data and the storage address of the function type of the calling function.

[0010] The above-mentioned storage format includes the String class; based on the data parameters of the target function and the storage format corresponding to the basic data, the steps of generating the initial basic data include: forcibly converting the data parameters of the target function into character pointer data corresponding to the target function; obtaining the data volume parameter of the character pointer data; and generating the initial basic data based on the character pointer data and the data volume parameter through the constructor of the String class.

[0011] In a second aspect, an embodiment of the present invention provides an asynchronous execution device for instructions, which includes: a thread startup module for responding to a call trigger event for a target thread and starting the target thread; a basic data receiving module for receiving, through the target thread, basic data of a target function corresponding to the instruction to be executed sent by a main thread; a call parameter determination module for determining, through the target thread, the call parameters of the target function based on the basic data of the target function; wherein the call parameters of the target function include data parameters and type parameters of the target function; an instruction execution module for controlling the target thread to call the target function based on the call parameters, and executing the instruction to be executed through the target function

[0012] In a third aspect, an embodiment of the present invention provides an electronic device including a processor and a memory, wherein the memory stores machine-executable instructions that can be executed by the processor, and the processor executes the machine-executable instructions to implement the above-mentioned asynchronous execution method of instructions.

[0013] In a fourth aspect, an embodiment of the present invention provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the asynchronous execution method of the above-mentioned instructions.

[0014] The embodiments of the present invention bring the following beneficial effects:

[0015] The aforementioned asynchronous instruction execution method, device, and electronic device initiate a target thread in response to a call trigger event targeting the target thread; receive, through the target thread, basic data of a target function corresponding to the instruction to be executed, sent by the main thread; determine, through the target thread, the target function's call parameters based on the target function's basic data; wherein the target function's call parameters include the target function's data parameters and type parameters; and control the target thread to call the target function based on the call parameters, thereby executing the instruction to be executed through the target function. This method executes the instruction to be executed by having the target thread call the anonymous function corresponding to the instruction to be executed, eliminating the need to port the code corresponding to the instruction to be executed through a non-anonymous function. This improves code writing efficiency and stability and reduces labor costs.

[0016] Other features and advantages of the present invention will be described in the following description, and in part will become apparent from the description, or understood by practicing the present invention. The purposes and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.

[0017] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the specific embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.

[0019] Figure 1 A flowchart of an asynchronous execution method of an instruction provided by an embodiment of the present invention;

[0020] Figure 2A schematic diagram of the complete data generation process of a Lambda function provided in an embodiment of the present invention;

[0021] Figure 3 A schematic diagram of receiving complete data of a Lambda function through a FIFO queue provided by an embodiment of the present invention;

[0022] Figure 4 A schematic structural diagram of an asynchronous execution device for instructions provided by an embodiment of the present invention;

[0023] Figure 5 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0024] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work shall fall within the scope of protection of the present invention.

[0025] When writing code, there are two common scenarios where you might want to offload code snippets to other threads. The first is when the API used by that code snippet is only allowed on a certain thread, or when the code requires significant resources to execute on the central processing unit (CPU). The process of offloading code snippets to other threads can be compared to writing lines 1, 2, 3, 4, and 5 of code and then wanting snippets 3 and 4 to be executed by other threads.

[0026] In most C++ programs, if you want to have a code snippet executed by another thread, you need to write a non-anonymous function, move the code snippet into the non-anonymous function, set a flag variable to trigger the function, and then the other thread will determine whether the variable is true in a loop. If so, it will call the function.

[0027] The above method requires writing a new function to re-encapsulate the code snippet. If the code snippet is heavily coupled with other code, such as using a local variable A from another code or a class member variable B, then it is necessary to define some new variables C in a structure and assign the variables AB to C. It is also necessary to rewrite all uses of AB in the code snippet to use C instead, in order to return to the re-encapsulation of the code snippet mentioned in step 1. Therefore, for C++ programs, having a code snippet executed by another thread is a relatively troublesome task, making the code bloated and difficult to maintain.

[0028] Based on this, an embodiment of the present invention provides a method, device, and electronic device for asynchronous execution of instructions. This technology can be applied to various scenarios where other threads are required to execute part of the code.

[0029] To facilitate understanding of this embodiment, first, an asynchronous execution method of an instruction disclosed in an embodiment of the present invention is described. Figure 1 As shown, the method includes the following steps:

[0030] Step 102: In response to a call triggering event for a target thread, start the target thread.

[0031] The aforementioned call triggering event can be the original thread executing a call instruction of the target thread. The original thread can be the main thread or a child thread other than the target thread. In a specific implementation, a programmer can add program code corresponding to the target thread's call instruction to the program code corresponding to the original thread, thereby causing the main thread to execute the call instruction during execution, causing the target thread to begin running.

[0032] The above-mentioned target thread can be a thread that specifically executes code to implement a certain function or functions. For example, when an Android program starts running, a process will be started. A user interface (UI) thread can be set in this process. When it comes to UI-related instructions, the UI thread is called to execute UI-related instructions separately. The above-mentioned target thread can also be a thread that only shares part of the code execution pressure of the main thread, and the functions of the code it executes are not limited. When writing code, relevant personnel can take into account the code execution pressure of the main thread and write code so that the instructions of part of the code are executed by calling a child thread. They can also write code so that the entire program collects CPU data processing information before calling the target thread and judges the current CPU's processing data saturation. If the saturation is high, the target thread can be called to execute part of the code.

[0033] There are many ways to call a thread, and the methods vary depending on the environment in which you run the program. For example, both the C++ and Java environments have corresponding thread call functions. These functions can be used to call a thread using pre-defined parameters, such as the thread name. This section will not be discussed in detail here.

[0034] Step 104: Receive, through the target thread, basic data of the target function corresponding to the instruction to be executed sent by the main thread.

[0035] The target function described above is typically constructed based on the instructions to be executed. Because non-anonymous functions create a function object and scope object when they are defined, variable conflicts may occur when the code to be executed by the target thread is highly coupled with the code executed by the main thread, making the code less stable. Anonymous functions, on the other hand, only temporarily create the function object and scope object when they are called and immediately release them after the call. This makes them more space-efficient than non-anonymous functions and prevents variable conflicts. Therefore, the target function described above is typically an anonymous function.

[0036] Common anonymous functions include Lambda functions, which can be used in both C++ and Java environments. Lambda function syntax consists of only one statement, making it relatively simple to rewrite the program code corresponding to the instructions to be executed into a Lambda function. A Lambda function statement can be expressed as:

[0037] lambda[arg1[,arg2,…argn]]:expression

[0038] Among them, arg1, arg2, ... argn are formal parameters, and expression is a function expression.

[0039] When the anonymous function is a Lambda function, since the Lambda function expression is untyped, to transmit different types of data to the target thread, the Lambda function expression can be converted to a common data type for storage. The Lambda function's function type can be inferred from the call result of calling the Lambda function using the template function in the current programming environment.

[0040] The above basic data usually includes the calling method and execution method of the target function, such as the name of the target function, the calling instruction, the input parameters of the target function, the expression of the function itself, the address of the target function and other information, which can be determined according to the definition process and type of the target function. If the target function needs to be called through other specified functions, the above basic data can also include relevant information of the specified function, such as the calling instruction, input parameters, and address of the specified function. In a specific way, since the function expression and function type of the anonymous function are needed when calling an anonymous function, the basic data of the target function sent by the main thread to the target thread needs to include data that can deduce the function expression and function type of the target function. Specifically, the basic data can include the target function expression and the storage address of the function used to determine the function type of the target function expression, so as to facilitate the subsequent target thread to call the target function. Among them, the target function expression and the storage address of the function used to determine its function type can be represented in the format of a common data type.

[0041] In addition, since the tasks that the target thread needs to perform may include more than one target function, a message queue can be set in the target thread to save the basic data of multiple target functions received. When the target thread needs to execute the target function corresponding to the basic data, the corresponding basic data can be retrieved from the message queue.

[0042] Step 106 : Based on the basic data of the target function, determine the calling parameters of the target function through the target thread; wherein the calling parameters of the target function include the data parameters and type parameters of the target function.

[0043] In a specific implementation, the target thread can parse the basic data to obtain the function expression of the target function and the data portion related to the function type of the target function expression. The target thread can then process the data portion related to the function type of the target function expression to obtain the function type of the target function. The function expression of the target function can serve as a data parameter of the target function, and the function type of the target function can serve as its type parameter.

[0044] Specifically, when the data part related to the function type of the function expression of the target function is the storage address of the function used to determine its function type, the function can be called based on the storage address of the function, so as to obtain the function type of the function expression of the target function through the function.

[0045] Step 108 : Control the target thread to call the target function based on the calling parameters, and execute the instructions to be executed through the target function.

[0046] Since the target function is constructed based on the instructions to be executed, when the target thread calls the target function through calling parameters, the instructions to be executed can be executed, thereby achieving the goal of handing over the program code corresponding to the instructions to be executed to the target thread.

[0047] The above-mentioned asynchronous instruction execution method responds to a call trigger event for a target thread and starts the target thread; receives, through the target thread, basic data of the target function corresponding to the instruction to be executed, sent by the main thread; based on the basic data of the target function, determines, through the target thread, the calling parameters of the target function; wherein the calling parameters of the target function include the data parameters and type parameters of the target function; and controls the target thread to call the target function based on the calling parameters, thereby executing the instruction to be executed through the target function. This method executes the instruction to be executed by having the target thread call the anonymous function corresponding to the instruction to be executed, eliminating the need to port the code corresponding to the instruction to be executed through a non-anonymous function, thereby improving code writing efficiency and stability and reducing labor costs.

[0048] The following embodiment provides an implementation method for determining the calling parameters of the target function through the target thread based on the basic data of the target function.

[0049] In the specific implementation process, if the code corresponding to the thread is running in a C++ environment, the basic data of the above target function can be generated in the following way:

[0050] (1) Obtaining the data parameters of the target function and the calling function corresponding to the target function; the calling function is constructed based on the data parameters of the target function and is used to determine the type parameters of the target function.

[0051] Specifically, when determining the type parameter of the target function by calling a function, the data parameter of the target function can be forcibly converted into corresponding character pointer data by calling the function, and the type of the character pointer data can be forcibly converted into the type parameter of the target function. Specifically, the above-mentioned calling function can be established based on the call template function.

[0052] (2) Generate initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data.

[0053] In specific implementations, the String class's data format can be used as the storage format. When generating the initial basic data, the target function's data parameters can be forcibly converted to the corresponding character pointer data. When the target function is a Lambda function, the Lambda function's expression can be forcibly converted (referred to as "forced conversion") to char* type data. The data size parameter of the character pointer data can then be obtained. This process can be implemented using the sizeof() function. Finally, the initial basic data is generated using the String class's constructor based on the character pointer data and the data size parameter.

[0054] (3) Generate basic data based on the storage address of the initial basic data and the function type of the calling function.

[0055] Specifically, the storage address of the function type of the calling function may be converted into data in the data format of the String class, and the converted data may be merged with the initial basic data to obtain the basic data.

[0056] After the target thread receives the basic data of the target function, it parses the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function, and controls the target thread to call the corresponding calling function through the storage address of the function type, and determines the type parameters of the target function through the calling function.

[0057] In the process of obtaining the storage address of the function type of the calling function, the storage address of the calling function type can be extracted from the basic data using a preset memory capacity measurement function based on the storage parameters of the storage address of the function type of the calling function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage location in the basic data. Specifically, the number of bytes of the storage address of the calling function type can be obtained using the sizeof() function when generating the basic data, and its storage location can be specifically set when generating the basic data.

[0058] The embodiment of the present invention also provides another asynchronous execution method of instructions. Figure 1 This method is mainly used in C++ programming environments and aims to achieve the following: for a code snippet, just enclose it in some symbols, without changing anything else, without creating a new non-anonymous function, and without caching context variables, so that another thread can execute it, thereby improving the efficiency of code transplantation.

[0059] This method uses C++ Lambda functions to encapsulate code snippets and store Lambda function data. It defines a FIFO queue in the target thread and passes the Lambda function data into the queue for processing.

[0060]

[0061]

[0062] If you need to simply encapsulate the code segments in lines 8 and 9 above without caching the local variable a and class member variable cptr used in the code segments, you can encapsulate them with a Lambda function as shown in the following code. However, it should be noted that in order for the Lambda expression to access or capture the variables a and cptr in the surrounding scope, the capture clause of the Lambda function must be [=].

[0063]

[0064] After obtaining the Lambda expression, it needs to be stored so that it can be transmitted to another thread for calling. Since Lambda expressions are non-specific types, they are syntactic sugar of C++11. Each defined Lambda expression is a new type. In order to transmit different types of data to another thread, the universal type std::string can be used to store the data of each Lambda expression.

[0065] Specifically, you only need to cast the lambda expression to char*, use sizeof to get its size, and pass it to the std::string constructor. However, calling a lambda expression requires not only the data but also type information to cast the char* back for calling.

[0066] In order to determine the type information of the Lambda expression, you can define a Call template function, the function type is the function type CallType that takes Lambda data as a parameter. First, use the c++ template to deduce the type of the Lambda expression. In the Call function, the data char* type of the Lambda expression is forcibly converted to the derived type of the Lambda expression. Since the function type has an address, this achieves the purpose of binding the type of the Lambda expression and the address of the Call function. Furthermore, the function address is also stored in std::string and merged with the Lambda expression, you can get a complete data containing type information and data information (equivalent to the above-mentioned "basic data"), the data type of the complete data is std::string, and the address of the Call function is in front and the Lambda data is in the back, such as Figure 2 As shown in the figure, there are three Lambda expression examples, and the code block in the Call function part is pseudo code used for example.

[0067] After obtaining the data, define a FIFO (First Input First Output) queue of type std::string in the thread to be called to receive and process the complete data of the Lambda expression. When the FIFO queue is not empty, take out a complete data from the end of the queue. Since the address size of the Call function can be obtained through sizeof, the Call address data of the corresponding size can be directly extracted and converted to the CallType function type before calling it. At the sending end, the complete Lambda data can be added to the end of the FIFO queue of the thread to be executed, as shown in the following example. Figure 3 shown.

[0068] The final pseudocode is as follows. You only need to wrap the code segment with two lines of code: DispatchToThreadBegin and DispatchToThreadEnd.

[0069]

[0070]

[0071] The above method enables C++ code writers to have code snippets executed by other threads with only two lines of code, thereby improving the efficiency of code transplantation and reducing labor costs.

[0072] For the above method embodiments, see Figure 4 An asynchronous execution device for an instruction shown, the device comprising:

[0073] The thread starting module 402 is used to respond to a call triggering event for a target thread and start the target thread;

[0074] The basic data receiving module 404 is used to receive the basic data of the target function corresponding to the instruction to be executed sent by the main thread through the target thread;

[0075] A calling parameter determination module 406 is configured to determine the calling parameters of the target function through the target thread based on the basic data of the target function; wherein the calling parameters of the target function include data parameters and type parameters of the target function;

[0076] The instruction execution module 408 is used to control the target thread to call the target function based on the calling parameters, and execute the to-be-executed instructions through the target function.

[0077] The aforementioned asynchronous execution device for instructions responds to a call trigger event targeting a target thread and starts the target thread. The target thread receives basic data of a target function corresponding to the instruction to be executed, sent by a main thread. Based on the basic data of the target function, the target thread determines the target function's calling parameters, including its data parameters and type parameters. The target thread is controlled to call the target function based on the calling parameters, and the target function executes the instruction to be executed. This method executes the instruction to be executed by having the target thread call the anonymous function corresponding to the instruction to be executed, eliminating the need to port the code corresponding to the instruction to be executed via a non-anonymous function. This improves code writing efficiency and stability and reduces labor costs.

[0078] The basic data of the above-mentioned target function includes the data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function and is used to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; the above-mentioned calling parameter determination module is also used to: parse the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; control the target thread to call the corresponding calling function through the storage address of the function type, and determine the type parameters of the target function through the calling function.

[0079] The above-mentioned calling parameter determination module is also used to: extract the storage address of the calling function type from the basic data based on the storage parameters of the storage address of the function type of the calling function through a preset memory capacity measurement function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage position in the basic data.

[0080] The calling parameter determination module is further used to: forcibly convert the data parameter of the target function into the corresponding character pointer data by calling the function, and forcibly convert the type of the character pointer data into the type parameter of the target function.

[0081] The basic data receiving module is further used to store the basic data of the target function into a message queue preset in the target thread.

[0082] The above-mentioned device also includes a basic data generation module, which is used to: obtain the data parameters of the target function and the calling function corresponding to the target function; the calling function is constructed based on the data parameters of the target function to determine the type parameters of the target function; based on the data parameters of the target function and the storage format corresponding to the basic data, generate initial basic data; based on the initial basic data and the storage address of the function type of the calling function, generate basic data.

[0083] The above storage format includes the String class; the above basic data generation module is also used to: forcibly convert the data parameters of the target function into character pointer data corresponding to the target function; obtain the data volume parameter of the character pointer data; and generate initial basic data based on the character pointer data and the data volume parameter through the constructor of the String class.

[0084] This embodiment further provides an electronic device, including a processor and a memory, wherein the memory stores machine-executable instructions that can be executed by the processor, and the processor executes the machine-executable instructions to implement the asynchronous execution method of the above instructions, for example:

[0085] In response to a call trigger event for a target thread, the target thread is started; basic data of a target function corresponding to the instruction to be executed sent by the main thread is received through the target thread; based on the basic data of the target function, the calling parameters of the target function are determined through the target thread; wherein the calling parameters of the target function include data parameters and type parameters of the target function; the target thread is controlled to call the target function based on the calling parameters, and the instruction to be executed is executed through the target function.

[0086] The above method executes the instruction to be executed by calling the anonymous function corresponding to the instruction to be executed through the target thread, without the need to transplant the code corresponding to the instruction to be executed through a non-anonymous function, thereby improving the code writing efficiency and stability and reducing labor costs.

[0087] Optionally, the basic data of the above-mentioned target function includes data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; based on the basic data of the target function, the step of determining the calling parameters of the target function through the target thread includes: parsing the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; controlling the target thread to call the corresponding calling function through the storage address of the function type, and determining the type parameters of the target function through the calling function.

[0088] Optionally, the above-mentioned step of parsing the basic data of the target function to obtain the storage address of the function type of the calling function includes: extracting the storage address of the calling function type from the basic data based on the storage parameters of the storage address of the function type of the calling function through a preset memory capacity measurement function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage position in the basic data.

[0089] Optionally, the above step of determining the type parameter of the target function by calling the function includes: forcibly converting the data parameter of the target function into corresponding character pointer data by calling the function, and forcibly converting the type of the character pointer data into the type parameter of the target function.

[0090] Optionally, the step of receiving, through the target thread, the preset basic data of the target function sent by the main thread includes: storing the basic data of the target function into a message queue preset in the target thread.

[0091] Optionally, the basic data of the above-mentioned objective function is generated in the following manner: obtaining the data parameters of the objective function and the calling function corresponding to the objective function; constructing the calling function based on the data parameters of the objective function to determine the type parameters of the objective function; generating initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data; generating basic data based on the initial basic data and the storage address of the function type of the calling function.

[0092] Optionally, the above storage format includes the String class; based on the data parameters of the target function and the storage format corresponding to the basic data, the steps of generating the initial basic data include: forcibly converting the data parameters of the target function into character pointer data corresponding to the target function; obtaining the data volume parameter of the character pointer data; and generating the initial basic data based on the character pointer data and the data volume parameter through the constructor of the String class.

[0093] See also Figure 5 As shown, the electronic device includes a processor 100 and a memory 101. The memory 101 stores machine-executable instructions that can be executed by the processor 100. The processor 100 executes the machine-executable instructions to implement the asynchronous execution method of the above instructions.

[0094] Furthermore, Figure 5 The electronic device shown further includes a bus 102 and a communication interface 103 , and the processor 100 , the communication interface 103 and the memory 101 are connected via the bus 102 .

[0095] The memory 101 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is achieved through at least one communication interface 103 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used. The bus 102 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or one type of bus.

[0096] The processor 100 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in the processor 100 or software instructions. The above processor 100 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present invention can be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium well-known in the art, such as a random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or register. The storage medium is located in the memory 101. The processor 100 reads the information in the memory 101 and, in conjunction with its hardware, completes the steps of the method of the aforementioned embodiment.

[0097] This embodiment further provides a machine-readable storage medium storing machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement an asynchronous execution method of the above instructions, for example:

[0098] In response to a call trigger event for a target thread, the target thread is started; basic data of a target function corresponding to the instruction to be executed sent by the main thread is received through the target thread; based on the basic data of the target function, the calling parameters of the target function are determined through the target thread; wherein the calling parameters of the target function include data parameters and type parameters of the target function; the target thread is controlled to call the target function based on the calling parameters, and the instruction to be executed is executed through the target function.

[0099] The above method executes the instruction to be executed by calling the anonymous function corresponding to the instruction to be executed through the target thread, without the need to transplant the code corresponding to the instruction to be executed through a non-anonymous function, thereby improving the code writing efficiency and stability and reducing labor costs.

[0100] Optionally, the basic data of the above-mentioned target function includes data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; based on the basic data of the target function, the step of determining the calling parameters of the target function through the target thread includes: parsing the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; controlling the target thread to call the corresponding calling function through the storage address of the function type, and determining the type parameters of the target function through the calling function.

[0101] Optionally, the above-mentioned step of parsing the basic data of the target function to obtain the storage address of the function type of the calling function includes: extracting the storage address of the calling function type from the basic data based on the storage parameters of the storage address of the function type of the calling function through a preset memory capacity measurement function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage position in the basic data.

[0102] Optionally, the above step of determining the type parameter of the target function by calling the function includes: forcibly converting the data parameter of the target function into corresponding character pointer data by calling the function, and forcibly converting the type of the character pointer data into the type parameter of the target function.

[0103] Optionally, the step of receiving, through the target thread, the preset basic data of the target function sent by the main thread includes: storing the basic data of the target function into a message queue preset in the target thread.

[0104] Optionally, the basic data of the above-mentioned objective function is generated in the following manner: obtaining the data parameters of the objective function and the calling function corresponding to the objective function; constructing the calling function based on the data parameters of the objective function to determine the type parameters of the objective function; generating initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data; generating basic data based on the initial basic data and the storage address of the function type of the calling function.

[0105] Optionally, the above storage format includes the String class; based on the data parameters of the target function and the storage format corresponding to the basic data, the steps of generating the initial basic data include: forcibly converting the data parameters of the target function into character pointer data corresponding to the target function; obtaining the data volume parameter of the character pointer data; and generating the initial basic data based on the character pointer data and the data volume parameter through the constructor of the String class.

[0106] An embodiment of the present invention provides an asynchronous execution method, device, and electronic device for instructions, including a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the method described in the previous method embodiment. The specific implementation can be found in the method embodiment and will not be repeated here.

[0107] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described systems and devices can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0108] In addition, in the description of the embodiments of the present invention, unless otherwise expressly specified or limited, the terms "mounted," "connected," and "connected" should be understood in a broad sense. For example, they may refer to fixed connections, detachable connections, or integral connections; they may refer to mechanical connections or electrical connections; they may refer to direct connections or indirect connections through an intermediate medium; and they may refer to internal communication between two components. Those skilled in the art will understand the specific meanings of the above terms in the present invention based on the specific circumstances.

[0109] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0110] In the description of the present invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings and are intended solely to facilitate and simplify the description of the present invention. They are not intended to indicate or imply that the devices or components referred to must have, be constructed, or operate in a specific orientation, and therefore should not be construed as limitations on the present invention. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.

[0111] Finally, it should be noted that the above embodiments are only specific implementation methods of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that any person skilled in the art can modify or easily conceive of changes to the technical solutions described in the above embodiments within the technical scope disclosed by the present invention, or replace some of the technical features therein with equivalents. Such modifications, changes or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be based on the scope of protection of the claims.

Claims

1. A method for asynchronous execution of instructions, characterized in that: The method comprises: In response to a call triggering event for a target thread, starting the target thread; Receive, through the target thread, basic data of the target function corresponding to the instruction to be executed sent by the main thread; Based on the basic data of the target function, determining the calling parameters of the target function through the target thread; wherein the calling parameters of the target function include data parameters and type parameters of the target function; Controlling the target thread to call the target function based on the calling parameter, and executing the to-be-executed instruction through the target function; The basic data of the target function includes the data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function and is used to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; The step of determining the calling parameters of the target function through the target thread based on the basic data of the target function includes: Parsing the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; The target thread is controlled to call a corresponding calling function through a storage address of the function type, and a type parameter of the target function is determined through the calling function.

2. The method according to claim 1, characterized in that The step of parsing the basic data of the target function to obtain the storage address of the function type of the calling function includes: The storage address of the calling function type is extracted from the basic data based on the storage parameters of the storage address of the function type of the calling function through a preset memory capacity measurement function; the storage parameters include the number of bytes of the storage address of the calling function type and the storage position in the basic data.

3. The method according to claim 1, characterized in that The step of determining the type parameter of the target function by the calling function includes: The data parameter of the target function is forcibly converted into corresponding character pointer data by calling a function, and the type of the character pointer data is forcibly converted into the type parameter of the target function.

4. The method according to claim 1, wherein The step of receiving, by the target thread, basic data of a preset target function sent by the main thread comprises: The basic data of the target function is stored in a message queue preset in the target thread.

5. The method according to claim 1, wherein The basic data of the objective function is generated in the following way: Obtaining data parameters of the objective function and a calling function corresponding to the objective function; the calling function is constructed based on the data parameters of the objective function and is used to determine the type parameters of the objective function; Generate initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data; Basic data is generated based on the initial basic data and a storage address of the function type of the calling function.

6. The method according to claim 5, characterized in that The storage format includes String class; The step of generating initial basic data based on the data parameters of the objective function and the storage format corresponding to the basic data includes: Forcibly converting the data parameter of the target function into character pointer data corresponding to the target function; Obtaining a data volume parameter of the character pointer data; Initial basic data is generated based on the character pointer data and the data amount parameter through the constructor of the String class.

7. An asynchronous execution device for instructions, characterized in that: The device comprises: A thread starting module, configured to respond to a call triggering event for a target thread and start the target thread; A basic data receiving module is used to receive, through the target thread, basic data of the target function corresponding to the instruction to be executed sent by the main thread; a calling parameter determination module, configured to determine the calling parameters of the target function through the target thread based on the basic data of the target function; wherein the calling parameters of the target function include data parameters and type parameters of the target function; An instruction execution module, configured to control the target thread to call the target function based on the calling parameters, and execute the to-be-executed instruction through the target function; The basic data of the target function includes the data parameters of the target function stored according to the set data type and the storage address of the function type of the calling function; the calling function is constructed based on the data parameters of the target function and is used to determine the type parameters of the target function; the calling parameters include the data parameters and type parameters of the target function; The calling parameter determination module is further used for: Parsing the basic data of the target function to obtain the storage address of the function type of the calling function corresponding to the target function and the data parameters of the target function; The target thread is controlled to call a corresponding calling function through a storage address of the function type, and a type parameter of the target function is determined through the calling function.

8. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores machine-executable instructions that can be executed by the processor, and the processor executes the machine-executable instructions to implement the asynchronous execution method of the instructions according to any one of claims 1 to 6.

9. A machine-readable storage medium, characterized in that The machine-readable storage medium stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the asynchronous execution method of instructions according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Log output method and device, storage medium and terminal

    CN110795303A