Message processing method and device
By introducing worker threads and message queues into multi-threaded message processing, synchronous or asynchronous processing is performed according to the message type, the problems of complex synchronization between threads, resource competition, and deadlock are solved, and the convenience and compatibility of message processing are improved.
Patent Information
- Application Number
- CN202110882353.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2018-05-15
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2038-05-15
AI Technical Summary
The multi-threaded message processing method in the prior art has complex synchronization between threads, resource competition and deadlock problems, and no effective solution has been proposed.
The message is retrieved from the head of the message queue through the worker thread and the corresponding callback function is called according to the message type (synchronous message or asynchronous message) for processing. After the synchronization message is processed, the next message is retrieved from the head of the message queue, and for the asynchronous message, the next message is retrieved without waiting for the processing result.
It realizes the details of hiding multi-threading and synchronization from users, improves the convenience and compatibility of message processing, improves the user experience and favorability, and solves the problems of complex synchronization between threads, resource competition, and deadlock.
Smart Images

Figure CN113656189B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of thread programming, and in particular to a message processing method and device. Background Art
[0002] In many software solutions, multithreaded programming is inevitably involved. Multithreading provides convenience for programming, but it also brings many problems. For example, Windows API is not fully compatible with POSIX standards, which makes some differences in threads and related APIs. Users need to encapsulate platform compatibility. There are currently three main solutions. The first is to use pthead to implement the library on Windows; the second is to use the latest C11 standard; the third is for users to distinguish the compile conditions by themselves, resulting in poor cross-platform multithreading. For example, synchronization issues between threads, especially when there are many thread modules, resource competition, deadlock and other problems often make program problems difficult to maintain and track.
[0003] The existing multi-threaded message processing methods have the problems of complex synchronization between threads, resource competition and deadlock, and no effective solution has been proposed so far. Summary of the invention
[0004] The embodiment of the present invention provides a message processing method and device to at least solve the technical problems of complex synchronization between threads, resource competition and deadlock in the multi-thread-based message processing method in the prior art.
[0005] According to one aspect of an embodiment of the present invention, a message processing method is provided, comprising: obtaining a first message from a head of a message queue through a working thread, wherein at least one message is stored in the message queue, and the message queue is constructed by a callback function corresponding to a user service; determining a message type of the first message through the working thread, wherein the message type includes: a synchronous message or an asynchronous message; in the case where the message type is a synchronous message, processing the first message by calling the callback function through the working thread, and after all the first messages are processed by the callback function, obtaining a second message from the head of the message queue through the working thread; in the case where the message type is an asynchronous message, processing the first message by calling the callback function through the working thread, and obtaining the second message from the head of the message queue through the working thread.
[0006] Further, calling the callback function through the working thread to process the first message includes: calling the callback function through the working thread and the message data of the first message to process the first message.
[0007] Furthermore, when the message type is a synchronous message, the first message is processed by calling a callback function through the working thread, including: waking up the synchronous calling thread through the working thread and allocating a first storage space for the synchronous calling thread; and calling the callback function through the synchronous calling thread to process the first message.
[0008] Furthermore, after waking up the synchronous calling thread through the working thread and allocating the first storage space for the synchronous calling thread, the above method also includes: storing the message data of the first message in the first storage space through the synchronous calling thread; and adding a new message to the end of the message queue through the synchronous calling thread.
[0009] Further, obtaining the second message from the head of the message queue through the working thread includes: waking up the working thread through the synchronous calling thread; and obtaining the second message from the head of the message queue through the working thread.
[0010] Furthermore, when the message type is an asynchronous message, the first message is processed by calling a callback function through the working thread, including: waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread; and calling the callback function through the asynchronous calling thread to process the first message.
[0011] Furthermore, after waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread, the above method also includes: storing the message data of the first message in the second storage space through the asynchronous calling thread; and adding a new message to the end of the message queue through the asynchronous calling thread.
[0012] Further, obtaining the second message from the head of the message queue through the working thread includes: waking up the working thread through the asynchronous calling thread; and obtaining the second message from the head of the message queue through the working thread.
[0013] Further, after the working thread is awakened by the asynchronous calling thread, the second storage space is released.
[0014] Furthermore, before obtaining the first message from the head of the message queue through the working thread, a message queue is constructed, wherein, before obtaining the second message from the head of the message queue through the working thread, the above method also includes: determining whether preset flag information is detected, wherein the preset flag information is used to indicate that the working thread stops obtaining the second message from the head of the message queue; if the preset flag information is detected, destructing the message queue; if the preset flag information is not detected, obtaining the second message from the head of the message queue through the working thread.
[0015] Furthermore, before obtaining the first message from the head of the message queue through the working thread, the method further includes: creating a working thread based on the callback function, and allocating a third storage space to the working thread.
[0016] Further, after the first message is obtained from the head of the message queue through the working thread, a fourth storage space is allocated to the first message through the working thread, and the message data of the first message is stored in the fourth storage space.
[0017] Furthermore, obtaining the first message or the second message from the head of the message queue through the working thread includes: determining whether there is a message in the message queue; and obtaining the first message or the second message from the head of the message queue through the working thread when it is determined that there is a message in the message queue.
[0018] Furthermore, obtaining the first message or the second message from the head of the message queue through the working thread includes: utilizing the mutual exclusion mechanism to obtain the first message or the second message from the head of the message queue through the working thread; adding a new message to the tail of the message queue through the synchronous calling thread or the asynchronous calling thread includes: utilizing the mutual exclusion mechanism to add a new message to the tail of the message queue through the synchronous calling thread or the asynchronous calling thread.
[0019] According to another aspect of an embodiment of the present invention, a message processing device is also provided, including: an acquisition module, used to acquire a first message from the head of a message queue through a working thread, wherein at least one message is stored in the message queue; a determination module, used to determine the message type of the first message through the working thread, wherein the message type includes: a synchronous message or an asynchronous message; a first calling module, used to process the first message by calling a callback function through the working thread when the message type is a synchronous message, and after all the first messages are processed by the callback function, acquire the second message from the head of the message queue through the working thread; a second calling module, used to process the first message by calling the callback function through the working thread when the message type is an asynchronous message, and acquire the second message from the head of the message queue through the working thread.
[0020] According to another aspect of an embodiment of the present invention, a storage medium is further provided. The storage medium includes a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute the above-mentioned message processing method.
[0021] According to another aspect of an embodiment of the present invention, a processor is further provided, and the processor is used to run a program, wherein the above-mentioned message processing method is executed when the program is running.
[0022] According to another aspect of an embodiment of the present invention, a message processing method is also provided, including: obtaining a first message from the head of a message queue through a working thread, wherein at least one message is stored in the message queue; processing the first message by calling a callback function through the working thread; and obtaining a second message from the head of the message queue through the working thread.
[0023] In an embodiment of the present invention, after obtaining the first message from the head of the message queue through the working thread, the message type of the first message can be first determined. In the case where the message type is a synchronous message, the first message is processed by calling a callback function through the working thread. After all the first messages are processed through the callback function, the second message is obtained from the head of the message queue through the working thread. In the case where the message type is an asynchronous message, the first message is processed by calling a callback function through the working thread, and the second message is obtained from the head of the message queue through the working thread, thereby realizing multi-threaded message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling a callback function through the working thread, and different operations can be performed on messages of different message types, thereby achieving the technical effect of hiding the details of multi-threading and its synchronization from the user, improving the convenience of message processing, improving the compatibility of message processing, and improving the user experience and favorability, thereby solving the technical problems of complex synchronization between threads, resource competition, and deadlock in the multi-threaded message processing method based on the prior art. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0025] Figure 1 is a flow chart of a message processing method according to an embodiment of the present invention;
[0026] Figure 2 is a flowchart of constructing an optional working thread according to an embodiment of the present invention;
[0027] Figure 3 is an implementation logic diagram of an optional message queue according to an embodiment of the present invention;
[0028] Figure 4 is an optional work thread operation flow chart according to an embodiment of the present invention;
[0029] Figure 5 is an optional message calling flow chart according to an embodiment of the present invention;
[0030] Figure 6is a schematic diagram of a message processing device according to an embodiment of the present invention; and
[0031] Figure 7 is a flowchart of another message processing method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0032] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0034] Example 1
[0035] According to an embodiment of the present invention, an embodiment of a message processing method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0036] Figure 1 is a flow chart of a message processing method according to an embodiment of the present invention. Figure 1 As shown, the method comprises the following steps:
[0037] Step S102: Obtain a first message from the head of a message queue through a working thread, wherein the message queue stores at least one message, and the message queue is constructed by a callback function corresponding to a user service.
[0038] Specifically, the above-mentioned working thread can be a pre-established thread for processing at least one message. By processing at least one message through the working thread, for the user, the effect of multi-threaded synchronous processing can be achieved, and the details of multi-threading and its synchronization are hidden, making it extremely convenient to use; the above-mentioned message queue can be a queue that is pre-established according to processing needs, and at least one message to be processed is stored in the message queue. The working thread realizes user-level multi-threaded synchronous processing by processing the messages stored in the message queue in sequence.
[0039] Step S104: determining the message type of the first message through the working thread, wherein the message type includes: synchronous message or asynchronous message.
[0040] Specifically, the messages stored in the message queue can be divided into synchronous messages and asynchronous messages. For synchronous messages, the worker thread needs to complete the execution of the synchronous message and obtain the execution result before executing other messages. For asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
[0041] Step S106, when the message type is a synchronous message, the first message is processed by calling the callback function through the working thread, and after all the first messages are processed by the callback function, the second message is obtained from the head of the message queue through the working thread.
[0042] Specifically, the callback function mentioned above may be a message processing function related to user services, which is called through a working thread and can process messages stored in a message queue.
[0043] Step S108, when the message type is an asynchronous message, the first message is processed by calling the callback function through the working thread, and the second message is obtained from the head of the message queue through the working thread.
[0044] It should be noted that the above-mentioned working thread, message queue and callback function can utilize the mature open source project libuv to implement cross-platform underlying support, reasonably package the business, and use a multi-threaded message communication framework written in pure C language. Specifically, the working thread is defined as follows; typedef voID*Handler; Handler newHandler(voID*obj,HandlerCallBack cb); the callback function is defined as follows: typedef int64_t(*HandlerCallBack)(voID*obj,int msg,int64_t wparam,int64_t lparam).
[0045] In an optional solution, users can use their own business-related callback functions to build a message queue, which stores multiple messages to be processed. Since only one message can be processed at a time through a working thread, a message at the head of the message queue can be obtained through the working thread, and it can be determined whether the message is a synchronous message or an asynchronous message. If the message is determined to be a synchronous message, the callback function can be called to process the message. After determining that the callback function has processed the message, other messages can be processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message without waiting for the execution result of the message, and other messages can be directly processed. Processing other messages can be achieved by the working thread obtaining a message from the head of the message queue again.
[0046] In the above embodiment of the present invention, after the first message is obtained from the head of the message queue by the working thread, the message type of the first message can be first determined. In the case where the message type is a synchronous message, the first message is processed by the working thread calling the callback function. After all the first messages are processed by the callback function, the second message is obtained from the head of the message queue by the working thread. In the case where the message type is an asynchronous message, the first message is processed by the working thread calling the callback function, and the second message is obtained from the head of the message queue by the working thread, thereby realizing multi-threaded message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function by the working thread, and different operations can be performed on messages of different message types, thereby achieving the technical effect of hiding the details of multi-threading and its synchronization from the user, improving the convenience of message processing, improving the compatibility of message processing, and improving the user experience and favorability, thereby solving the technical problems of complex synchronization between threads, resource competition, and deadlock in the prior art based on multi-threaded message processing methods.
[0047] Optionally, calling the callback function to process the first message through the working thread includes: calling the callback function to process the first message through the working thread and the message data of the first message.
[0048] Specifically, the above-mentioned message data may be a message identifier (eg, message ID) and parameters (eg, abstract parameters).
[0049] In an optional solution, the user provides a message identifier and parameters, and the working thread can call a callback function to process the first message corresponding to the message identifier and parameters to obtain a processing result.
[0050] Optionally, when the message type is a synchronous message, the first message is processed by calling a callback function through the working thread, including: waking up the synchronous calling thread through the working thread and allocating a first storage space for the synchronous calling thread; and calling the callback function through the synchronous calling thread to process the first message.
[0051] Specifically, the first storage space mentioned above may be a stack memory, which may be used to store message data of a synchronization message.
[0052] In an optional solution, for synchronous messages, the synchronous message API can be used, that is, the queue object is passed by the synchronous calling thread. Specifically, stack memory is used, and a synchronization object is created, the synchronous calling thread is awakened, and the first message is processed by the synchronous calling thread.
[0053] It should be noted that the synchronous call thread is defined as follows: int64_t sendSyncMsg(Handler h, intID, int64_t wparam, int64_t lparam).
[0054] Optionally, after waking up the synchronous calling thread through the working thread and allocating the first storage space for the synchronous calling thread, the above method also includes: storing the message data of the first message in the first storage space through the synchronous calling thread; and adding a new message to the end of the message queue through the synchronous calling thread.
[0055] In an alternative solution, for synchronous messages, the message data can be stored in stack memory and a message can be added to the end of the message queue.
[0056] Optionally, obtaining the second message from the head of the message queue through the working thread includes: waking up the working thread through a synchronous calling thread; and obtaining the second message from the head of the message queue through the working thread.
[0057] In an optional solution, after the synchronous message is processed, the condition variable can be activated and the working thread can be awakened. The working thread continues to obtain messages from the head of the message queue to obtain the second message, and continues to process the second message.
[0058] Optionally, when the message type is an asynchronous message, the first message is processed by calling a callback function through the working thread, including: waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread; and calling the callback function through the asynchronous calling thread to process the first message.
[0059] Specifically, the second storage space mentioned above may be a message body memory, which is used to store message data of asynchronous messages.
[0060] In an optional solution, for asynchronous messages, the queue object can be passed through the asynchronous message API, that is, through the asynchronous call function. Specifically, the message body memory is used to wake up the asynchronous call thread, and the first message is processed through the asynchronous call thread.
[0061] It should be noted that the asynchronous call thread is defined as follows: void sendAsyncMsg(Handler h, int ID, int64_t wparam, int64_t lparam).
[0062] Optionally, after waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread, the above method also includes: storing the message data of the first message in the second storage space through the asynchronous calling thread; and adding a new message to the end of the message queue through the asynchronous calling thread.
[0063] In an alternative solution, for asynchronous messages, the message data can be stored in the message body memory and a message can be added to the end of the message queue.
[0064] Optionally, obtaining the second message from the head of the message queue through the working thread includes: waking up the working thread through an asynchronous calling thread; and obtaining the second message from the head of the message queue through the working thread.
[0065] In an optional solution, while calling the callback function through an asynchronous calling thread, there is no need to wait for the processing result of the asynchronous message, and other messages can be directly processed. That is, the conditional variable can be activated and the working thread can be woken up. The working thread continues to obtain messages from the head of the message queue, obtains the second message, and continues to process the second message.
[0066] Optionally, after the working thread is awakened by the asynchronous calling thread, the second storage space is released.
[0067] In an optional solution, in order to avoid wasting storage space, while the callback function is processing the asynchronous message, the message body memory storing the message data of the asynchronous message can be stored.
[0068] Optionally, before obtaining the first message from the head of the message queue through the working thread, a message queue is constructed, wherein, before obtaining the second message from the head of the message queue through the working thread, the above method also includes: determining whether preset flag information is detected, wherein the preset flag information is used to indicate that the working thread stops obtaining the second message from the head of the message queue; if the preset flag information is detected, destructing the message queue; if the preset flag information is not detected, obtaining the second message from the head of the message queue through the working thread.
[0069] Specifically, the above-mentioned preset flag information may be an exit flag, which is used to indicate that the message queue has been used up.
[0070] In an optional solution, before obtaining a message from a message queue and processing it, it is first determined whether the message has been used up. If not, the message can be obtained from the message queue; if it has been used up, the message queue can be directly destroyed to end the message processing process.
[0071] It should be noted that the destruction message queue is defined as follows: void freeHandler (Handler handler).
[0072] Optionally, before obtaining the first message from the head of the message queue through the working thread, the method further includes: creating a working thread based on the callback function, and allocating a third storage space to the working thread.
[0073] Specifically, the third storage space mentioned above may be the memory of the working thread, which is used to store message objects and callback functions related to the working thread.
[0074] In an alternative approach, Figure 2 As shown, the creation process of a working thread is as follows: input parameters such as message object and callback function, apply for object memory, save message object and callback function, initialize mutex and condition variable objects, and create a working thread.
[0075] Optionally, after the first message is obtained from the head of the message queue through the working thread, a fourth storage space is allocated to the first message through the working thread, and message data of the first message is stored in the fourth storage space.
[0076] Specifically, the fourth storage space mentioned above may be a message memory for storing message data.
[0077] In an optional solution, message memory may be applied for and message data may be saved according to the message ID and parameters.
[0078] Optionally, obtaining the first message or the second message from the head of the message queue through the working thread includes: determining whether there is a message in the message queue; and obtaining the first message or the second message from the head of the message queue through the working thread when it is determined that there is a message in the message queue.
[0079] In an optional solution, in order to avoid the situation where the message queue is not stored and the working thread works normally, it is possible to first determine whether there is a message in the message queue, that is, to determine whether the message queue is empty. If the message queue is not empty, it can be determined that there is a message in the message queue, and the message is obtained from the head of the message queue to obtain the first message or the second message; if the message queue is empty, it is possible to wait for the message to be stored in the message queue, and then obtain the message.
[0080] Optionally, obtaining the first message or the second message from the head of the message queue through the working thread includes: utilizing the mutual exclusion mechanism to obtain the first message or the second message from the head of the message queue through the working thread; adding a new message to the tail of the message queue through the synchronous calling thread or the asynchronous calling thread includes: utilizing the mutual exclusion mechanism to add a new message to the tail of the message queue through the synchronous calling thread or the asynchronous calling thread.
[0081] In an optional solution, when you need to get a message from a message queue, you can use the mutual exclusion mechanism to enter the mutual exclusion area and read a message from the head of the message queue, and then leave the mutual exclusion area to complete the process of getting the message from the message queue; when you need to add a message to the end of the message queue, you can enter the mutual exclusion area, add a message to the end of the message queue, and then leave the mutual exclusion area to complete the process of adding the message to the message queue.
[0082] Figure 3 is an implementation logic diagram of an optional message queue according to an embodiment of the present invention. Figure 4 is an optional work thread operation flow chart according to an embodiment of the present invention, Figure 5 This is an optional message call flow chart according to an embodiment of the present invention. Figures 3 to 5 A preferred embodiment of the present invention is described in detail.
[0083] like Figure 3 As shown, the user can provide a message processing callback function and use the callback function to construct a message queue, as well as synchronously or asynchronously call the message queue. When the message queue is used up, the message queue is destroyed.
[0084] like Figure 4As shown, after the working thread runs, it can first detect the exit flag. If the exit flag is detected, the operation ends; if the exit flag is not detected, it can enter the mutually exclusive area and determine whether the message queue is empty. If the message queue is determined to be empty, it directly leaves the mutually exclusive area; if the message queue is not empty, a valid message is taken from the head of the message queue and the mutually exclusive area is left. Further determine whether the message is successfully obtained. If it is not successfully obtained, that is, the message queue is empty, the next loop is started; if it is successfully obtained, that is, the message queue is not empty, the message object and message data are used to call the callback function, and determine whether the obtained message is a synchronous message. If it is a synchronous message, the synchronization object is activated, the synchronous calling thread is awakened, and the next loop is started; if it is an asynchronous message, the message body memory is released, and the next loop is started. The execution process of the next loop is the same as the above process, which will not be repeated here.
[0085] like Figure 5 As shown in the figure, the message calling process during the operation of the worker thread is as follows: after successfully obtaining the message and calling the callback function, the workflow can enter the message id and parameters, apply for the message body memory and save the message data, and determine whether the obtained message is a synchronous message. If it is a synchronous message, use the stack memory and create a synchronization object to further save the message data; if it is an asynchronous message, apply for the message body memory and save the message data. After the message data is saved, enter the mutex area, add a message to the end of the message queue, leave the mutex area, activate the condition variable, and wake up the worker thread. Further determine whether it is a synchronous message. If it is a synchronous message, no processing is required. If it is not a synchronous message, leave the mutex area, activate the condition variable, and wake up the worker thread.
[0086] Through the above solution, the mature open source project libuv is used to achieve cross-platform underlying support, and the business is reasonably packaged to provide an extremely convenient pure C multi-threaded message communication framework for users. Its API is also extremely simple, with four APIs for creation, destruction, synchronous messaging, and asynchronous messaging. The details of multi-threading and its synchronization are hidden from users, making it extremely convenient to use.
[0087] Example 2
[0088] According to an embodiment of the present invention, an embodiment of a message processing device is provided.
[0089] Figure 6 is a schematic diagram of a message processing device according to an embodiment of the present invention. Figure 6 As shown, the device comprises:
[0090] The acquisition module 62 is used to acquire the first message from the head of the message queue through a working thread, wherein the message queue stores at least one message, and the message queue is constructed by a callback function corresponding to the user service.
[0091] Specifically, the above-mentioned working thread can be a pre-established thread for processing at least one message. By processing at least one message through the working thread, for the user, the effect of multi-threaded synchronous processing can be achieved, and the details of multi-threading and its synchronization are hidden, making it extremely convenient to use; the above-mentioned message queue can be a queue that is pre-established according to processing needs, and at least one message to be processed is stored in the message queue. The working thread realizes user-level multi-threaded synchronous processing by processing the messages stored in the message queue in sequence.
[0092] The determination module 64 is used to determine the message type of the first message through the working thread, wherein the message type includes: synchronous message or asynchronous message.
[0093] Specifically, the messages stored in the message queue can be divided into synchronous messages and asynchronous messages. For synchronous messages, the worker thread needs to complete the execution of the synchronous message and obtain the execution result before executing other messages. For asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
[0094] The first calling module 66 is used to process the first message by calling the callback function through the working thread when the message type is a synchronous message, and obtain the second message from the head of the message queue through the working thread after all the first messages are processed by the callback function.
[0095] Specifically, the callback function mentioned above may be a message processing function related to user services, which is called through a working thread and can process messages stored in a message queue.
[0096] The second calling module 68 is used to process the first message by calling the callback function through the working thread when the message type is an asynchronous message, and obtain the second message from the head of the message queue through the working thread.
[0097] It should be noted that the above-mentioned working thread, message queue and callback function can utilize the mature open source project libuv to implement cross-platform underlying support, reasonably package the business, and use a multi-threaded message communication framework written in pure C language. Specifically, the working thread is defined as follows; typedef voID*Handler; Handler newHandler(voID*obj,HandlerCallBack cb); the callback function is defined as follows: typedef int64_t(*HandlerCallBack)(voID*obj,int msg,int64_t wparam,int64_t lparam).
[0098] In an optional solution, users can use their own business-related callback functions to build a message queue, which stores multiple messages to be processed. Since only one message can be processed at a time through a working thread, a message at the head of the message queue can be obtained through the working thread, and it can be determined whether the message is a synchronous message or an asynchronous message. If the message is determined to be a synchronous message, the callback function can be called to process the message. After determining that the callback function has processed the message, other messages can be processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message without waiting for the execution result of the message, and other messages can be directly processed. Processing other messages can be achieved by the working thread obtaining a message from the head of the message queue again.
[0099] In the above embodiment of the present invention, after the first message is obtained from the head of the message queue by the working thread, the message type of the first message can be first determined. In the case where the message type is a synchronous message, the first message is processed by the working thread calling the callback function. After all the first messages are processed by the callback function, the second message is obtained from the head of the message queue by the working thread. In the case where the message type is an asynchronous message, the first message is processed by the working thread calling the callback function, and the second message is obtained from the head of the message queue by the working thread, thereby realizing multi-threaded message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function by the working thread, and different operations can be performed on messages of different message types, thereby achieving the technical effect of hiding the details of multi-threading and its synchronization from the user, improving the convenience of message processing, improving the compatibility of message processing, and improving the user experience and favorability, thereby solving the technical problems of complex synchronization between threads, resource competition, and deadlock in the prior art based on multi-threaded message processing methods.
[0100] Optionally, the first calling module or the second calling module is further used to call the callback function to process the first message through the working thread and the message data of the first message.
[0101] Optionally, the first calling module also includes: a first wake-up unit, used to wake up the synchronous calling thread through the working thread when the message type is a synchronous message, and allocate a first storage space for the synchronous calling thread; the first calling unit, used to process the first message by calling the callback function through the synchronous calling thread.
[0102] Optionally, the first calling module also includes: a first storage unit, used to store the message data of the first message in the first storage space through the synchronous calling thread; and a first adding unit, used to add a new message to the end of the message queue through the synchronous calling thread.
[0103] Optionally, the first calling module further includes: a second awakening unit, used to awaken the working thread through a synchronous calling thread; and a first acquiring unit, used to acquire the second message from the head of the message queue through the working thread.
[0104] Optionally, the second calling module also includes: a third wake-up unit, used to wake up the asynchronous calling thread through the working thread when the message type is an asynchronous message, and allocate a second storage space for the asynchronous calling thread; a second calling unit, used to call the callback function through the asynchronous calling thread to process the first message.
[0105] Optionally, the second calling module further includes: a second storage unit, used to store the message data of the first message in the second storage space through the asynchronous calling thread; and a second adding unit, used to add a new message to the end of the message queue through the asynchronous calling thread.
[0106] Optionally, the second calling module further includes: a fourth awakening unit, used to awaken the working thread through the asynchronous calling thread; and a second acquiring unit, used to acquire the second message from the head of the message queue through the working thread.
[0107] Optionally, the second calling module further includes: a releasing unit, configured to release the second storage space after waking up the working thread through the asynchronous calling thread.
[0108] Optionally, the device also includes: a construction module, used to construct a message queue before obtaining the first message from the head of the message queue through the working thread, wherein the device also includes: a judgment module, used to judge whether preset flag information is detected before obtaining the second message from the head of the message queue through the working thread, wherein the preset flag information is used to indicate that the working thread stops obtaining the second message from the head of the message queue; a destruction module, used to destroy the message queue if the preset flag information is detected; the acquisition module is also used to obtain the second message from the head of the message queue through the working thread if the preset flag information is not detected.
[0109] Optionally, the device further includes: a creation module, configured to create a working thread based on the callback function and allocate a third storage space to the working thread before obtaining the first message from the head of the message queue through the working thread.
[0110] Optionally, the device further includes: an allocation module, configured to allocate a fourth storage space for the first message through the working thread after acquiring the first message from the head of the message queue through the working thread, and store the message data of the first message in the fourth storage space.
[0111] Optionally, the acquisition module, the first calling module or the second calling module also includes: a judgment unit, used to determine whether there is a message in the message queue; the first acquisition unit, the second acquisition unit or the third acquisition unit is also used to obtain the first message or the second message from the head of the message queue through a working thread when it is determined that there is a message in the message queue.
[0112] Optionally, the first acquisition unit, the second acquisition unit or the third acquisition unit is also used to utilize the mutual exclusion mechanism to obtain the first message or the second message from the head of the message queue through the working thread; the first adding unit or the second adding unit is also used to utilize the mutual exclusion mechanism to add a new message to the tail of the message queue through the synchronous calling thread or the asynchronous calling thread.
[0113] Example 3
[0114] According to an embodiment of the present invention, an embodiment of a message processing method is also provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0115] Figure 7 is a flowchart of another message processing method according to an embodiment of the present invention. Figure 7 As shown, the method comprises the following steps:
[0116] Step S702: Obtain a first message from the head of a message queue through a working thread, wherein the message queue stores at least one message.
[0117] Specifically, the above-mentioned working thread can be a pre-established thread for processing at least one message. By processing at least one message through the working thread, for the user, the effect of multi-threaded synchronous processing can be achieved, and the details of multi-threading and its synchronization are hidden, making it extremely convenient to use; the above-mentioned message queue can be a queue that is pre-established according to processing needs, and at least one message to be processed is stored in the message queue. The working thread realizes user-level multi-threaded synchronous processing by processing the messages stored in the message queue in sequence.
[0118] Step S704: Process the first message by calling the callback function through the working thread.
[0119] Specifically, the callback function mentioned above may be a message processing function related to user services, which is called through a working thread and can process messages stored in a message queue.
[0120] Step S706: Obtain the second message from the head of the message queue through the working thread.
[0121] Specifically, the messages stored in the message queue can be divided into synchronous messages and asynchronous messages. For synchronous messages, the worker thread needs to complete the execution of the synchronous message and obtain the execution result before executing other messages. For asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
[0122] It should be noted that the above-mentioned working thread, message queue and callback function can utilize the mature open source project libuv to implement cross-platform underlying support, reasonably package the business, and use a multi-threaded message communication framework written in pure C language. Specifically, the working thread is defined as follows; typedef voID*Handler; Handler newHandler(voID*obj,HandlerCallBack cb); the callback function is defined as follows: typedef int64_t(*HandlerCallBack)(voID*obj,int msg,int64_t wparam,int64_t lparam).
[0123] In an optional solution, users can use their own business-related callback functions to build a message queue, which stores multiple messages to be processed. Since only one message can be processed at a time through a working thread, a message at the head of the message queue can be obtained through the working thread, and it can be determined whether the message is a synchronous message or an asynchronous message. If the message is determined to be a synchronous message, the callback function can be called to process the message. After determining that the callback function has processed the message, other messages can be processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message without waiting for the execution result of the message, and other messages can be directly processed. Processing other messages can be achieved by the working thread obtaining a message from the head of the message queue again.
[0124] In the above embodiment of the present invention, after the first message is obtained from the head of the message queue by the working thread, the first message can be processed by calling the callback function by the working thread, and the second message can be obtained from the head of the message queue by the working thread. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function by the working thread, and different operations can be performed on messages of different message types, thereby achieving the technical effect of hiding the details of multi-threading and its synchronization from the user, improving the convenience of message processing, improving the compatibility of message processing, and improving the user experience and favorability, thereby solving the technical problems of complex synchronization between threads, resource competition, and deadlock in the prior art based on multi-threaded message processing methods.
[0125] Example 4
[0126] According to an embodiment of the present invention, there is also provided an embodiment of a storage medium, wherein the storage medium includes a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute the message processing methods of the above-mentioned embodiments 1 and 3.
[0127] Example 5
[0128] According to an embodiment of the present invention, an embodiment of a processor is also provided, and the processor is used to run a program, wherein the message processing methods of the above embodiments 1 and 3 are executed when the program is running.
[0129] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0130] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0131] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0132] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0133] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0134] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0135] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A message processing method, characterized in that: include: Obtaining a first message from a head of a message queue through a working thread, wherein the message queue stores at least one message, and the message queue is constructed by a callback function corresponding to a user service, and the callback function is a message processing function related to the user service, and is used to process the message stored in the message queue by calling the working thread; Determining a message type of the first message through the working thread, wherein the message type includes: a synchronous message or an asynchronous message; In the case where the message type is the synchronous message, the first message is processed by the worker thread by calling a callback function, and after all the first messages are processed by the callback function, a second message is obtained from the head of the message queue by the worker thread; In the case where the message type is the asynchronous message, the first message is processed by calling the callback function through the working thread, and the second message is obtained from the head of the message queue through the working thread.
2. The method according to claim 1, characterized in that: The process of calling the callback function by the working thread to process the first message includes: The callback function is called to process the first message through the working thread and the message data of the first message.
3. The method according to claim 2, characterized in that When the message type is the synchronous message, processing the first message by calling a callback function through the worker thread includes: Waking up the synchronous calling thread through the working thread and allocating a first storage space for the synchronous calling thread; The callback function is called by the synchronous calling thread to process the first message.
4. The method according to claim 3, characterized in that After waking up the synchronous calling thread through the working thread and allocating a first storage space for the synchronous calling thread, the method further includes: storing the message data of the first message in the first storage space through the synchronous calling thread; A new message is added to the tail of the message queue through the synchronous calling thread.
5. The method according to claim 3, characterized in that: Obtaining the second message from the head of the message queue by the working thread includes: Waking up the working thread through the synchronous calling thread; The second message is obtained from the head of the message queue through the working thread.
6. The method according to claim 2, characterized in that In the case where the message type is the asynchronous message, processing the first message by calling a callback function through the worker thread includes: Waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread; The callback function is called by the asynchronous calling thread to process the first message.
7. The method according to claim 6, characterized in that After waking up the asynchronous calling thread through the working thread and allocating a second storage space for the asynchronous calling thread, the method further includes: storing the message data of the first message in the second storage space through the asynchronous calling thread; A new message is added to the tail of the message queue through the asynchronous calling thread.
8. The method according to claim 6, characterized in that Obtaining the second message from the head of the message queue by the working thread includes: Waking up the working thread through the asynchronous calling thread; The second message is obtained from the head of the message queue through the working thread.
9. The method according to claim 8, characterized in that After the working thread is awakened by the asynchronous calling thread, the second storage space is released.
10. The method according to claim 1, characterized in that Before obtaining the first message from the head of the message queue by the working thread, constructing the message queue, wherein before obtaining the second message from the head of the message queue by the working thread, the method further includes: Determining whether preset flag information is detected, wherein the preset flag information is used to indicate that the working thread stops obtaining the second message from the head of the message queue; If the preset flag information is detected, destructing the message queue; If the preset flag information is not detected, the second message is obtained from the head of the message queue through the working thread.
11. The method according to claim 1, characterized in that: Before obtaining the first message from the head of the message queue through the working thread, the method further includes: The working thread is created based on the callback function, and a third storage space is allocated to the working thread.
12. A message processing device, characterized in that: include: An acquisition module, used for acquiring a first message from a head of a message queue through a working thread, wherein the message queue stores at least one message, the message queue is constructed by a callback function corresponding to a user service, the callback function is a message processing function related to the user service, and is used for processing the message stored in the message queue by calling a working thread; A determination module, configured to determine a message type of the first message through the working thread, wherein the message type includes: a synchronous message or an asynchronous message; A first calling module is used for, when the message type is the synchronous message, calling the callback function through the working thread to process the first message, and after the callback function has processed all the first messages, obtaining a second message from the head of the message queue through the working thread; The second calling module is used to call the callback function through the working thread to process the first message when the message type is the asynchronous message, and obtain the second message from the head of the message queue through the working thread.
13. A message processing method, characterized in that: include: Obtaining a first message from a head of a message queue through a working thread, wherein the message queue stores at least one message, and the message queue is constructed by a callback function corresponding to a user service, and the callback function is a message processing function related to the user service, and is used to process the message stored in the message queue by calling the working thread; Calling a callback function to process the first message through the working thread; The second message is obtained from the head of the message queue through the working thread.
Citation Information
Patent Citations
Method for implementing asynchronous communication
CN102946376A