CN108717380B - Message processing method and device - Google Patents

Message processing method and device Download PDF

Info

Publication number
CN108717380B
CN108717380B CN201810463425.4A CN201810463425A CN108717380B CN 108717380 B CN108717380 B CN 108717380B CN 201810463425 A CN201810463425 A CN 201810463425A CN 108717380 B CN108717380 B CN 108717380B
Authority
CN
China
Prior art keywords
message
thread
calling
queue
head
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201810463425.4A
Other languages
Chinese (zh)
Other versions
CN108717380A (en
Inventor
杨星亮
王官军
苏睿
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xian Wanxiang Electronics Technology Co Ltd
Original Assignee
Xian Wanxiang Electronics Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Xian Wanxiang Electronics Technology Co Ltd filed Critical Xian Wanxiang Electronics Technology Co Ltd
Priority to CN201810463425.4A priority Critical patent/CN108717380B/en
Priority to CN202110882353.9A priority patent/CN113656189A/en
Publication of CN108717380A publication Critical patent/CN108717380A/en
Application granted granted Critical
Publication of CN108717380B publication Critical patent/CN108717380B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues

Abstract

The invention discloses a message processing method and a message processing device. Wherein, the method comprises the following steps: acquiring 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, and the at least one message is a message needing parallel processing; determining, by the worker thread, a message type of the first message, the message type comprising: synchronous messages or asynchronous messages; under the condition that the message type is a synchronous message, calling a callback function through a working thread to process the first message, and acquiring a second message from the head of a message queue through the working thread after all the first messages are processed through the callback function; and under the condition that the message type is asynchronous, calling a callback function through the working thread to process the first message, and acquiring a second message from the head of the message queue through the working thread. The invention solves the technical problems of complicated synchronization among threads, resource competition and deadlock in the message processing method based on multithreading in the prior art.

Description

Message processing method and device
Technical Field
The present invention relates to the field of thread programming, and in particular, to a method and an apparatus for processing a message.
Background
In many software solutions, multithread programming is inevitably involved, which provides convenience for programming but also brings many problems, for example, windows api is not completely compatible with posix standard, which causes some differences in threads and related api, and requires users to perform platform-compatible encapsulation themselves, and at present, there are three main solutions, the first is to use pthead implementation library in windows; the second is to use the latest c11 standard; the third is that the user compiles condition differentiation by himself, which causes the multithreading to cross platform to be poor. For another example, inter-thread synchronization problems, especially when there are many thread modules, often make program problems difficult to maintain and track due to resource contention, deadlock, and the like
Aiming at the problems of complex synchronization among threads, resource competition and deadlock in the message processing method based on multithreading in the prior art, an effective solution is not provided at present.
Disclosure of Invention
The embodiment of the invention provides a message processing method and a message processing device, which at least solve the technical problems of complex synchronization among threads, resource competition and deadlock in a message processing method based on multiple threads in the prior art.
According to an aspect of an embodiment of the present invention, there is provided a message processing method, including: acquiring 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; determining, by the worker thread, a message type of the first message, wherein the message type comprises: synchronous messages or asynchronous messages; under the condition that the message type is a synchronous message, calling a callback function through a working thread to process the first message, and acquiring a second message from the head of a message queue through the working thread after all the first messages are processed through the callback function; and under the condition that the message type is asynchronous, calling a callback function through the working thread to process the first message, and acquiring a second message from the head of the message queue through the working thread.
Further, the calling back function is called by the working thread to process the first message, and the processing comprises the following steps: and calling a callback function to process the first message through the working thread and the message data of the first message.
Further, in the case that the message type is a synchronization message, the processing the first message by calling the callback function through the worker thread includes: awakening the synchronous calling thread through the working thread, and distributing a first storage space for the synchronous calling thread; and calling a callback function through the synchronous calling thread to process the first message.
Further, after waking up the synchronous call thread through the working thread and allocating the first storage space for the synchronous call thread, the method further includes: storing message data of the first message in a first storage space through a synchronous calling thread; and adding a new message at the tail part of the message queue through the synchronous calling thread.
Further, obtaining, by the worker thread, the second message from the head of the message queue includes: awakening a working thread through a synchronous calling thread; the second message is retrieved from the head of the message queue by the worker thread.
Further, in the case that the message type is an asynchronous message, the processing the first message by calling the callback function through the worker thread includes: awakening the asynchronous calling thread through the working thread and distributing a second storage space for the asynchronous calling thread; and calling a callback function through the asynchronous calling thread to process the first message.
Further, 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 message data of the first message in a second storage space through the asynchronous calling thread; and adding a new message at the tail part of the message queue through the asynchronous calling thread.
Further, obtaining, by the worker thread, the second message from the head of the message queue includes: awakening a working thread through an asynchronous calling thread; the second message is retrieved from the head of the message queue by the worker thread.
Further, the second memory space is released after the worker thread is woken up by the asynchronous call thread.
Further, before the first message is obtained from the head of the message queue through the worker thread, constructing the message queue, wherein before the second message is obtained from the head of the message queue through the worker thread, the method further comprises: judging whether preset mark information is detected or not, wherein the preset mark information is used for representing that the working thread stops acquiring a second message from the head of the message queue; if the preset mark information is detected, a message queue is constructed; and if the preset mark information is not detected, acquiring a second message from the head of the message queue through the working thread.
Further, before the first message is obtained from the head of the message queue through the worker thread, the method further includes: and creating a working thread based on the callback function, and allocating a third storage space for the working thread.
Further, after the first message is acquired from the head of the message queue by the worker thread, a fourth storage space is allocated for the first message by the worker thread, and the message data of the first message is stored in the fourth storage space.
Further, obtaining, by the worker thread, the first message or the second message from the head of the message queue includes: judging whether a message exists in the message queue or not; and in the case that the message exists in the message queue, acquiring the first message or the second message from the head of the message queue through the working thread.
Further, obtaining, by the worker thread, the first message or the second message from the head of the message queue includes: acquiring a first message or a second message from the head of a message queue through a working thread by using a mutual exclusion mechanism; adding a new message at the tail of the message queue through a synchronous calling thread or an asynchronous calling thread comprises the following steps: and adding a new message at the tail part of the message queue by using a mutual exclusion mechanism through a synchronous calling thread or an asynchronous calling thread.
According to another aspect of the embodiments of the present invention, there is also provided a message processing apparatus, including: the system comprises an acquisition module, a processing module and a processing module, wherein the acquisition module is used for acquiring a first message from the head of a message queue through a working thread, and at least one message is stored in the message queue; a determining module, configured to determine a message type of the first message through the worker thread, where the message type includes: synchronous messages or asynchronous messages; the first calling module is used for calling the callback function through the working thread to process the first message under the condition that the message type is the synchronous message, and acquiring a second message from the head of the message queue through the working thread after all the first messages are processed through the callback function; and the second calling module is used for calling the callback function through the working thread to process the first message under the condition that the message type is the asynchronous message, and acquiring the second message from the head of the message queue through the working thread.
According to another aspect of the embodiments of the present invention, there is also provided a storage medium including a stored program, wherein when the program runs, a device on which the storage medium is located is controlled to execute the message processing method described above.
According to another aspect of the embodiments of the present invention, there is also provided a processor, where the processor is configured to execute a program, where the program executes the message processing method described above.
According to another aspect of the embodiments of the present invention, there is also provided a message processing method, including: acquiring 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; calling a callback function through a working thread to process the first message; the second message is retrieved from the head of the message queue by the worker thread.
In the embodiment of the present invention, after a first message is acquired from the head of a message queue through a work thread, the message type of the first message may be first determined, in a case that the message type is a synchronous message, a callback function is called through the work thread to process the first message, in a case that all the first messages are completely processed through the callback function, a second message is acquired from the head of the message queue through the work thread, in a case that the message type is an asynchronous message, the callback function is called through the work thread to process the first message, and the second message is acquired from the head of the message queue through the work thread, thereby implementing multi-thread message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function through the working thread, and different operations can be executed on messages of different message types, so that the technical effects of hiding multiple threads and synchronization details thereof for a user, improving the convenience of message processing, improving the compatibility of message processing, improving the user experience and good sensitivity are achieved, and the technical problems of complex synchronization among threads, resource competition and deadlock in the message processing method based on multiple threads in the prior art are solved.
Drawings
The accompanying drawings, which are included to provide a further understanding of the invention and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the invention and together with the description serve to explain the invention without limiting the invention. In the drawings:
FIG. 1 is a flow chart of a message processing method according to an embodiment of the invention;
FIG. 2 is a flow diagram of the construction of an alternative worker thread according to an embodiment of the invention;
FIG. 3 is a logic diagram of an implementation of an alternative message queue according to an embodiment of the present invention;
FIG. 4 is a flow diagram of an alternative worker thread operation according to an embodiment of the present invention;
FIG. 5 is an alternative message invocation flow diagram according to an embodiment of the present invention;
FIG. 6 is a schematic diagram of a message processing apparatus according to an embodiment of the present invention; and
fig. 7 is a flowchart of another message processing method according to an embodiment of the present invention.
Detailed Description
In order to make the technical solutions of the present invention better understood, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that the terms "first," "second," and the like in the description and claims of the present invention and in the drawings described above are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the invention described herein are capable of operation in sequences other than those illustrated or described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
Example 1
In accordance with an embodiment of the present invention, there is provided an embodiment of a message processing method, it should be noted that the steps illustrated in the flowchart of the accompanying drawings may be performed in a computer system such as a set of computer-executable instructions, and that while a logical order is illustrated in the flowchart, in some cases the steps illustrated or described may be performed in an order different than here.
Fig. 1 is a flowchart of a message processing method according to an embodiment of the present invention, as shown in fig. 1, the method includes the following steps:
step S102, a first message is obtained from the head of a message queue through a working thread, wherein at least one message is stored in the message queue.
Specifically, the above-mentioned working thread may be a thread which is established in advance and used for processing at least one message, and the at least one message is processed through the working thread, so that for a user, the effect of multi-thread synchronous processing can be realized, and multi-threads and details of synchronization thereof are hidden, so that the use is very convenient; the message queue can be a queue which is pre-established according to the processing requirement, at least one message to be processed is stored in the message queue, and the working thread processes the messages stored in the message queue in sequence, so that the user-level multi-thread synchronous processing is realized.
Step S104, determining the message type of the first message through the working thread, wherein the message type comprises: synchronous messages or asynchronous messages.
Specifically, the messages stored in the message queue may be divided into synchronous messages and asynchronous messages, for synchronous messages, the worker thread needs to execute other messages after the synchronous messages are executed and the execution result is obtained, and for asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
And step S106, under the condition that the message type is the synchronous message, calling a callback function through the working thread to process the first message, and after all the first messages are processed through the callback function, acquiring a second message from the head of the message queue through the working thread.
Specifically, the callback function may be a message processing function related to user services, and may process a message stored in a message queue by being called by a worker thread.
And step S108, under the condition that the message type is asynchronous, calling a callback function through the working thread to process the first message, and acquiring a second message from the head of the message queue through the working thread.
It should be noted that the above-mentioned working thread, message queue and callback function may be a multi-thread message communication framework that uses mature open source project libuv to implement cross-platform bottom support, packages the service reasonably, and writes through pure C language, specifically, the working thread is defined as follows; typedef voID Handler; handler newHandler (voID obj, handlecallback cb); the callback function is defined as follows: typedef int64_ t (. multidirerlback) (voID _. obj, int msg, int64_ t wparam, int64_ t lparam).
In an optional scheme, a user can utilize a callback function related to own service to construct a message queue, a plurality of messages to be processed are stored in the message queue, only one message can be processed through a working thread, one message at the head of the message queue can be obtained through the working thread, whether the message is a synchronous message or an asynchronous message is judged, if the message is determined to be the synchronous message, the callback function can be called to process the message, and after the callback function is determined to process the message, other messages can be continuously processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message, and other messages can be directly processed without waiting for the execution result of the message. Processing other messages may be accomplished by the worker thread retrieving a message again from the head of the message queue.
In the above embodiment of the present invention, after acquiring the first message from the head of the message queue through the work thread, the message type of the first message may be first determined, in a case that the message type is a synchronous message, the first message is processed by calling the callback function through the work thread, after all the first messages are processed by the callback function, the second message is acquired from the head of the message queue through the work thread, in a case that the message type is an asynchronous message, the first message is processed by calling the callback function through the work thread, and the second message is acquired from the head of the message queue through the work thread, thereby implementing the multi-thread message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function through the working thread, and different operations can be executed on messages of different message types, so that the technical effects of hiding multiple threads and synchronization details thereof for a user, improving the convenience of message processing, improving the compatibility of message processing, improving the user experience and good sensitivity are achieved, and the technical problems of complex synchronization among threads, resource competition and deadlock in the message processing method based on multiple threads in the prior art are solved.
Optionally, the calling the callback function by the worker thread to process the first message includes: and calling a callback function to process the first message through the working thread and the message data of the first message.
Specifically, the message data described above may be a message identification (e.g., message ID) and a parameter (e.g., abstract parameter).
In an optional scheme, a user provides a message identifier and a parameter, and a callback function can be called by a working thread to process a first message corresponding to the message identifier and the parameter, so as to obtain a processing result.
Optionally, in a case that the message type is a synchronization message, the processing the first message by calling the callback function through the worker thread includes: awakening the synchronous calling thread through the working thread, and distributing a first storage space for the synchronous calling thread; and calling a callback function through the synchronous calling thread to process the first message.
Specifically, the first storage space may be a stack memory, and may be configured to store message data of the synchronization message.
In an optional scheme, for the synchronization message, the queue object may be passed through the synchronization message api, that is, the synchronous call thread, specifically, a stack memory is used, the synchronization object is created, the synchronous call thread is woken up, and the first message is processed through the synchronous call thread.
It should be noted that the synchronous call thread is defined as follows: int64_ t sendSyncMsg (Handler h, int ID, int64_ t wparam, int64_ t lparam).
Optionally, after waking up the synchronous call thread through the worker thread and allocating the first storage space for the synchronous call thread, the method further includes: storing message data of the first message in a first storage space through a synchronous calling thread; and adding a new message at the tail part of the message queue through the synchronous calling thread.
In an alternative, for synchronous messages, the message data may be stored in the stack memory, and a message may be added at the end of the message queue.
Optionally, obtaining, by the worker thread, the second message from the head of the message queue includes: awakening a working thread through a synchronous calling thread; the second message is retrieved from the head of the message queue by the worker thread.
In an optional scheme, after the synchronous message is processed, the condition variable may be activated, the worker thread is awakened, the worker thread continues to acquire the message from the head of the message queue to obtain the second message, and the second message is continuously processed.
Optionally, in a case that the message type is an asynchronous message, the processing the first message by calling the callback function through the worker thread includes: awakening the asynchronous calling thread through the working thread and distributing a second storage space for the asynchronous calling thread; and calling a callback function through the asynchronous calling thread to process the first message.
Specifically, the second storage space may be a message body memory, and is used to store message data of the asynchronous message.
In an alternative scheme, for an asynchronous message, the queue object may be passed through the asynchronous message api, that is, through an asynchronous call function, specifically, the asynchronous call thread is woken up using the message body memory, and the first message is processed through the asynchronous call thread.
It should be noted that the asynchronous calling thread is defined as follows: VOID sendaSyncMSg (Handler h, int ID, int64_ t wparam, int64_ t lparam).
Optionally, after waking up the asynchronous calling thread through the worker thread and allocating a second storage space for the asynchronous calling thread, the method further includes: storing message data of the first message in a second storage space through the asynchronous calling thread; and adding a new message at the tail part of the message queue through the asynchronous calling thread.
In an alternative, for asynchronous messages, the message data may be stored in the message body memory and a message may be added at the end of the message queue.
Optionally, obtaining, by the worker thread, the second message from the head of the message queue includes: awakening a working thread through an asynchronous calling thread; the second message is retrieved from the head of the message queue by the worker thread.
In an optional scheme, while the callback function is called by the asynchronous call thread, the processing result of the asynchronous message does not need to be waited, other messages can be directly processed continuously, that is, the condition variable can be activated, the working thread is waken up, the message is continuously acquired from the head of the message queue through the working thread, the second message is obtained, and the second message is continuously processed.
Optionally, the second memory space is released after the worker thread is awakened by the asynchronous call thread.
In an alternative scheme, to avoid the waste of memory space, while the callback function processes the asynchronous message, the message body memory storing the message data of the asynchronous message may be stored
Optionally, before the first message is obtained from the head of the message queue through the worker thread, constructing the message queue, wherein before the second message is obtained from the head of the message queue through the worker thread, the method further includes: judging whether preset mark information is detected or not, wherein the preset mark information is used for representing that the working thread stops acquiring a second message from the head of the message queue; if the preset mark information is detected, a message queue is constructed; and if the preset mark information is not detected, acquiring a second message from the head of the message queue through the working thread.
Specifically, the preset flag information may be an exit flag, which is used to indicate that the message queue is used up.
In an optional scheme, before obtaining and processing a message from a message queue, it is first determined whether a message pair is used completely, and if not, the message can be obtained from the message queue; if the message queue is used up, the message queue can be directly destructed, and the message processing process is finished.
It should be noted that the destructed message queue is defined as follows: VOID freeHandler (Handler Handler).
Optionally, before the first message is obtained from the head of the message queue through the worker thread, the method further includes: and creating a working thread based on the callback function, and allocating a third storage space for the working thread.
Specifically, the third storage space may be a memory of the worker thread, and is used for storing a message object and a callback function related to the worker thread.
In an alternative scheme, as shown in fig. 2, the creation process of the worker thread is as follows: inputting parameters such as a message object, a callback function and the like, applying for an object memory, storing the message object and the callback function, initializing a mutex and a condition variable object, and creating a working thread.
Optionally, after the first message is acquired from the head of the message queue by the worker thread, a fourth storage space is allocated for the first message by the worker thread, and the message data of the first message is stored in the fourth storage space.
Specifically, the fourth storage space may be a message memory, and is used for storing message data.
In an alternative scheme, the message memory may be applied and the message data may be stored according to the message ID and the parameter.
Optionally, the obtaining, by the worker thread, the first message or the second message from the head of the message queue includes: judging whether a message exists in the message queue or not; and in the case that the message exists in the message queue, acquiring the first message or the second message from the head of the message queue through the working thread.
In an optional scheme, in order to avoid that no message is stored in the message queue, the working thread works normally, and first, whether a message exists in the message queue or not can be judged, that is, whether the message queue is empty or not can be judged, if the message queue is not empty, the message exists in the message queue can be determined, and a first message or a second message is obtained by acquiring a message from the head of the message queue; if the message queue is empty, it may wait for the message to be stored in the message queue and then retrieve the message.
Optionally, the obtaining, by the worker thread, the first message or the second message from the head of the message queue includes: acquiring a first message or a second message from the head of a message queue through a working thread by using a mutual exclusion mechanism; adding a new message at the tail of the message queue through a synchronous calling thread or an asynchronous calling thread comprises the following steps: and adding a new message at the tail part of the message queue by using a mutual exclusion mechanism through a synchronous calling thread or an asynchronous calling thread.
In an optional scheme, when a message needs to be acquired from a message queue, a mutual exclusion mechanism can be used to enter a mutual exclusion area, read a message from the head of the message queue, and leave the mutual exclusion area to complete the process of acquiring the message from the message queue; when a message needs to be added at the tail of the message queue, the process of adding the message into the message queue can be completed by entering the mutual exclusion area, adding a message at the tail of the message queue and leaving the mutual exclusion area.
Fig. 3 is a logic diagram of an implementation of an optional message queue according to an embodiment of the present invention, fig. 4 is a flowchart of an optional workflow according to an embodiment of the present invention, and fig. 5 is a flowchart of an optional message call according to an embodiment of the present invention. A preferred embodiment of the present invention will be described in detail with reference to fig. 3 to 5.
As shown in fig. 3, a user may provide a message processing callback function and construct a message queue using the callback function, and call the message queue with a synchronous or asynchronous message, and when the message queue is used up, the message queue is constructed.
As shown in fig. 4, after the working thread runs, the exit flag may be detected first, and if the exit flag is detected, the running is ended; if the exit mark is not detected, entering a mutual exclusion area, judging whether the message queue is empty, and if the message queue is determined to be empty, directly leaving the mutual exclusion area; if the message queue is not empty, a valid message is fetched from the head of the message queue and leaves the mutex area. Further judging whether the message is successfully fetched or not, if the message is not successfully fetched, namely the message queue is empty, starting to execute the next cycle; if the message queue is successfully obtained, namely the message queue is not empty, calling a callback function by using the message object and the message data, judging whether the obtained message is a synchronous message, if so, activating the synchronous object, awakening the synchronous calling thread, and starting to execute the next cycle; if the message is asynchronous, the message body memory is released and the next cycle is started. The next loop execution process is the same as the above process, and is not described herein.
As shown in fig. 5, the message call flow in the working thread running process is specifically as follows: after the workflow successfully acquires the message and calls the callback function, the workflow can input a message id and parameters, apply for message body memory and store message data, judge whether the acquired message is a synchronous message, if the acquired message is the synchronous message, use stack memory, create a synchronous object and further store the message data; and if the message is an asynchronous message, applying for a message body memory and storing message data. After the message data is stored, entering a mutual exclusion area, adding a message at the tail of a message queue, leaving the mutual exclusion area, activating a condition variable and awakening a working thread. And further judging whether the synchronous message is the synchronous message, if the synchronous message is the synchronous message, processing is not needed, if the synchronous message is not the synchronous message, the mutual exclusion area is left, the condition variable is activated, and the working thread is awakened.
Through the scheme, the mature open source project libuv is utilized to realize the bottom support of the cross-platform, the service is reasonably packaged and provided for using a convenient pure c multithreading message communication framework, the api is also very simple, the establishment and the reconstruction are carried out, the synchronous message and the asynchronous message are four apis, the multithreading and the synchronous details of the multithreading are hidden for the user, and the use is very convenient.
Example 2
According to an embodiment of the present invention, there is provided an embodiment of a message processing apparatus.
Fig. 6 is a schematic diagram of a message processing apparatus according to an embodiment of the present invention, as shown in fig. 6, the apparatus includes:
the obtaining module 62 is configured to obtain a first message from a head of a message queue through a worker thread, where at least one message is stored in the message queue.
Specifically, the above-mentioned working thread may be a thread which is established in advance and used for processing at least one message, and the at least one message is processed through the working thread, so that for a user, the effect of multi-thread synchronous processing can be realized, and multi-threads and details of synchronization thereof are hidden, so that the use is very convenient; the message queue can be a queue which is pre-established according to the processing requirement, at least one message to be processed is stored in the message queue, and the working thread processes the messages stored in the message queue in sequence, so that the user-level multi-thread synchronous processing is realized.
A determining module 64, configured to determine a message type of the first message through the worker thread, where the message type includes: synchronous messages or asynchronous messages.
Specifically, the messages stored in the message queue may be divided into synchronous messages and asynchronous messages, for synchronous messages, the worker thread needs to execute other messages after the synchronous messages are executed and the execution result is obtained, and for asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
And the first calling module 66 is configured to, in a case that the message type is a synchronization message, call the callback function through the worker thread to process the first message, and after all the first messages are processed through the callback function, obtain, through the worker thread, the second message from the head of the message queue.
Specifically, the callback function may be a message processing function related to user services, and may process a message stored in a message queue by being called by a worker thread.
And the second calling module 68 is configured to, in a case that the message type is an asynchronous message, call the callback function through the worker thread to process the first message, and obtain the second message from the head of the message queue through the worker thread.
It should be noted that the above-mentioned working thread, message queue and callback function may be a multi-thread message communication framework that uses mature open source project libuv to implement cross-platform bottom support, packages the service reasonably, and writes through pure C language, specifically, the working thread is defined as follows; typedef voID Handler; handler newHandler (voID obj, handlecallback cb); the callback function is defined as follows: typedef int64_ t (. multidirerlback) (voID _. obj, int msg, int64_ t wparam, int64_ t lparam).
In an optional scheme, a user can utilize a callback function related to own service to construct a message queue, a plurality of messages to be processed are stored in the message queue, only one message can be processed through a working thread, one message at the head of the message queue can be obtained through the working thread, whether the message is a synchronous message or an asynchronous message is judged, if the message is determined to be the synchronous message, the callback function can be called to process the message, and after the callback function is determined to process the message, other messages can be continuously processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message, and other messages can be directly processed without waiting for the execution result of the message. Processing other messages may be accomplished by the worker thread retrieving a message again from the head of the message queue.
In the above embodiment of the present invention, after acquiring the first message from the head of the message queue through the work thread, the message type of the first message may be first determined, in a case that the message type is a synchronous message, the first message is processed by calling the callback function through the work thread, after all the first messages are processed by the callback function, the second message is acquired from the head of the message queue through the work thread, in a case that the message type is an asynchronous message, the first message is processed by calling the callback function through the work thread, and the second message is acquired from the head of the message queue through the work thread, thereby implementing the multi-thread message processing. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function through the working thread, and different operations can be executed on messages of different message types, so that the technical effects of hiding multiple threads and synchronization details thereof for a user, improving the convenience of message processing, improving the compatibility of message processing, improving the user experience and good sensitivity are achieved, and the technical problems of complex synchronization among threads, resource competition and deadlock in the message processing method based on multiple threads in the prior art are solved.
Optionally, the first calling module or the second calling module is further configured to call a callback function to process the first message through the work thread and the message data of the first message.
Optionally, the first calling module further includes: the first awakening unit is used for awakening the synchronous calling thread through the working thread under the condition that the message type is the synchronous message, and distributing a first storage space for the synchronous calling thread; and the first calling unit is used for calling the callback function through the synchronous calling thread to process the first message.
Optionally, the first calling module further includes: the first storage unit is used for storing the message data of the first message in a first storage space through a synchronous calling thread; and the first adding unit is used for adding a new message at the tail part of the message queue through the synchronous calling thread.
Optionally, the first calling module further includes: the second awakening unit is used for awakening the working thread through the synchronous calling thread; and the first acquisition unit is used for acquiring the second message from the head of the message queue through the working thread.
Optionally, the second calling module further includes: the third awakening unit is used for awakening the asynchronous calling thread through the working thread under the condition that the message type is the asynchronous message, and distributing a second storage space for the asynchronous calling thread; and the second calling unit is used for calling the callback function through the asynchronous calling thread to process the first message.
Optionally, the second calling module further includes: the second storage unit is used for storing the message data of the first message in a second storage space through the asynchronous calling thread; and the second adding unit is used for adding a new message at the tail part of the message queue through the asynchronous calling thread.
Optionally, the second calling module further includes: the fourth awakening unit is used for awakening the working thread through the asynchronous calling thread; and the second acquisition unit is used for acquiring a second message from the head of the message queue through the working thread.
Optionally, the second calling module further includes: and the releasing unit is used for releasing the second storage space after the work thread is awakened through the asynchronous calling thread.
Optionally, the apparatus further comprises: a construction module for constructing a message queue before a first message is fetched from a head of the message queue by a worker thread, wherein the apparatus further comprises: the judging module is used for judging whether preset mark information is detected or not before a second message is acquired from the head of the message queue through the working thread, wherein the preset mark information is used for representing that the working thread stops acquiring the second message from the head of the message queue; the deconstruction module is used for deconstructing the message queue if the preset mark information is detected; the obtaining module is further configured to obtain a second message from the head of the message queue through the worker thread if the preset flag information is not detected.
Optionally, the apparatus further comprises: and the creating module is used for creating the working thread based on the callback function and allocating a third storage space for the working thread before the first message is acquired from the head of the message queue through the working thread.
Optionally, the apparatus further comprises: and the allocation module is used for allocating a fourth storage space for the first message through the working thread after the first message is acquired from the head of the message queue through the working thread, and storing the message data of the first message in the fourth storage space.
Optionally, the obtaining module, the first calling module, or the second calling module further includes: the judging unit is used for judging whether a message exists in the message queue or not; the first obtaining unit, the second obtaining unit or the third obtaining unit is further configured to obtain the first message or the second message from the head of the message queue through the worker thread in a case that it is determined that the message exists in the message queue.
Optionally, the first obtaining unit, the second obtaining unit, or the third obtaining unit is further configured to obtain, by using a mutual exclusion mechanism, the first message or the second message from the head of the message queue through the worker thread; the first adding unit or the second adding unit is also used for adding a new message at the tail part of the message queue through a synchronous calling thread or an asynchronous calling thread by using a mutual exclusion mechanism.
Example 3
There is also provided, in accordance with an embodiment of the present invention, an embodiment of a message processing method, it should be noted that the steps illustrated in the flowchart of the accompanying drawings may be performed in a computer system such as a set of computer-executable instructions, and that, although a logical order is illustrated in the flowchart, in some cases, the steps illustrated or described may be performed in an order different than here.
Fig. 7 is a flowchart of another message processing method according to an embodiment of the present invention, as shown in fig. 7, the method includes the following steps:
step S702, a first message is obtained from the head of a message queue through a work thread, wherein at least one message is stored in the message queue.
Specifically, the above-mentioned working thread may be a thread which is established in advance and used for processing at least one message, and the at least one message is processed through the working thread, so that for a user, the effect of multi-thread synchronous processing can be realized, and multi-threads and details of synchronization thereof are hidden, so that the use is very convenient; the message queue can be a queue which is pre-established according to the processing requirement, at least one message to be processed is stored in the message queue, and the working thread processes the messages stored in the message queue in sequence, so that the user-level multi-thread synchronous processing is realized.
Step S704, the callback function is called by the worker thread to process the first message.
Specifically, the callback function may be a message processing function related to user services, and may process a message stored in a message queue by being called by a worker thread.
Step S706, a second message is obtained from the head of the message queue through the worker thread.
Specifically, the messages stored in the message queue may be divided into synchronous messages and asynchronous messages, for synchronous messages, the worker thread needs to execute other messages after the synchronous messages are executed and the execution result is obtained, and for asynchronous messages, the worker thread can execute other messages without waiting for the execution result.
It should be noted that the above-mentioned working thread, message queue and callback function may be a multi-thread message communication framework that uses mature open source project libuv to implement cross-platform bottom support, packages the service reasonably, and writes through pure C language, specifically, the working thread is defined as follows; typedef voID Handler; handler newHandler (voID obj, handlecallback cb); the callback function is defined as follows: typedef int64_ t (. multidirerlback) (voID _. obj, int msg, int64_ t wparam, int64_ t lparam).
In an optional scheme, a user can utilize a callback function related to own service to construct a message queue, a plurality of messages to be processed are stored in the message queue, only one message can be processed through a working thread, one message at the head of the message queue can be obtained through the working thread, whether the message is a synchronous message or an asynchronous message is judged, if the message is determined to be the synchronous message, the callback function can be called to process the message, and after the callback function is determined to process the message, other messages can be continuously processed; if the message is determined to be an asynchronous message, the callback function can be called to process the message, and other messages can be directly processed without waiting for the execution result of the message. Processing other messages may be accomplished by the worker thread retrieving a message again from the head of the message queue.
In the above embodiment of the present invention, after the first message is obtained from the head of the message queue through the worker thread, the first message may be processed by calling the callback function through the worker thread, and the second message is obtained from the head of the message queue through the worker thread. Compared with the prior art, the first message stored in the message queue is processed only by calling the callback function through the working thread, and different operations can be executed on messages of different message types, so that the technical effects of hiding multiple threads and synchronization details thereof for a user, improving the convenience of message processing, improving the compatibility of message processing, improving the user experience and good sensitivity are achieved, and the technical problems of complex synchronization among threads, resource competition and deadlock in the message processing method based on multiple threads in the prior art are solved.
Example 4
According to an embodiment of the present invention, there is also provided an embodiment of a storage medium, where the storage medium includes a stored program, and the device on which the storage medium is located is controlled to execute the message processing methods of embodiments 1 and 3 when the program runs.
Example 5
According to an embodiment of the present invention, an embodiment of a processor is further provided, where the processor is configured to execute a program, and the program executes the message processing method according to embodiments 1 and 3 when running.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
In the above embodiments of the present invention, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
In the embodiments provided in the present application, it should be understood that the disclosed technology can be implemented in other ways. The above-described embodiments of the apparatus are merely illustrative, and for example, the division of the units may be a logical division, and in actual implementation, there may be another division, for example, multiple units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, units or modules, and may be in an electrical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a removable hard disk, a magnetic or optical disk, and other various media capable of storing program codes.
The foregoing is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and decorations can be made without departing from the principle of the present invention, and these modifications and decorations should also be regarded as the protection scope of the present invention.

Claims (13)

1. A message processing method, comprising:
acquiring 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, and the message queue is constructed by a callback function corresponding to user service;
determining, by the worker thread, a message type of the first message, wherein the message type comprises: synchronous messages or asynchronous messages;
under the condition that the message type is the synchronous message, calling the callback function through the working thread to process the first message, and after all the first messages are processed through the callback function, acquiring a second message from the head of the message queue through the working thread;
under the condition that the message type is the asynchronous message, calling the callback function through the working thread to process the first message, and acquiring the second message from the head of the message queue through the working thread;
wherein obtaining the first message or the second message from the head of the message queue by the worker thread comprises: acquiring the first message or the second message from the head of a message queue through the working thread by using a mutual exclusion mechanism; adding a new message at the tail of the message queue through a synchronous calling thread or an asynchronous calling thread comprises the following steps: and adding a new message at the tail part of the message queue through the synchronous calling thread or the asynchronous calling thread by using a mutual exclusion mechanism.
2. The method of claim 1, wherein processing the first message by the worker thread invoking a callback function comprises:
and calling the callback function to process the first message through a working thread and the message data of the first message.
3. The method of claim 2, wherein, in the case that the message type is the synchronization message, processing the first message by the worker thread calling a callback function comprises:
awakening a synchronous calling thread through the working thread, and distributing a first storage space for the synchronous calling thread;
and calling the callback function through the synchronous calling thread to process the first message.
4. The method of claim 3, wherein after waking up a synchronous call thread by the worker thread and allocating a first memory space for the synchronous call thread, the method further comprises:
storing message data of the first message in the first storage space through the synchronous calling thread;
and adding a new message at the tail part of the message queue through the synchronous calling thread.
5. The method of claim 3, wherein retrieving, by the worker thread, a second message from a head of the message queue comprises:
awakening the working thread through the synchronous calling thread;
and acquiring the second message from the head of the message queue through the working thread.
6. The method of claim 2, wherein, in the case that the message type is the asynchronous message, processing the first message by the worker thread calling a callback function comprises:
awakening an asynchronous calling thread through the working thread and distributing a second storage space for the asynchronous calling thread;
and calling the callback function through the asynchronous calling thread to process the first message.
7. The method of claim 6, wherein after waking up an asynchronously calling thread via the worker thread and allocating a second memory space for the asynchronously calling thread, the method further comprises:
storing, by the asynchronous call thread, message data of the first message in the second memory space;
and adding a new message at the tail part of the message queue through the asynchronous calling thread.
8. The method of claim 6, wherein obtaining, by the worker thread, the second message from the head of the message queue comprises:
awakening the working thread through the asynchronous calling thread;
and acquiring the second message from the head of the message queue through the working thread.
9. The method of claim 8, wherein the second memory space is released after the worker thread is awakened by the asynchronous call thread.
10. The method of claim 1, wherein the message queue is constructed prior to retrieving a first message from a head of the message queue by a worker thread, wherein the method further comprises, prior to retrieving the second message from the head of the message queue by the worker thread:
judging whether preset mark information is detected or not, wherein the preset mark information is used for representing that the working thread stops acquiring the second message from the head of the message queue;
if the preset mark information is detected, the message queue is constructed;
and if the preset mark information is not detected, acquiring the second message from the head of the message queue through the working thread.
11. The method of claim 1, wherein prior to retrieving the first message from the head of the message queue by the worker thread, the method further comprises:
and creating the working thread based on the callback function, and allocating a third storage space for the working thread.
12. A message processing apparatus, comprising:
the system comprises an acquisition module, a processing module and a processing module, wherein the acquisition module is used for acquiring a first message from the head of a message queue through a working thread, at least one message is stored in the message queue, and the message queue is constructed by a callback function corresponding to user service;
a determining module, configured to determine, by the worker thread, a message type of the first message, where the message type includes: synchronous messages or asynchronous messages;
a first calling module, configured to, when the message type is the synchronization message, call the callback function through the worker thread to process the first message, and after all the first messages are processed through the callback function, obtain, through the worker thread, a second message from the head of the message queue;
the second calling module is used for calling the callback function through the working thread to process the first message under the condition that the message type is the asynchronous message, and acquiring a second message from the head of the message queue through the working thread;
the processing device further comprises a first obtaining unit and a second obtaining unit, which are used for obtaining the first message or the second message from the head of a message queue through the working thread by using a mutual exclusion mechanism; the first adding unit or the second adding unit is further configured to add a new message at the tail of the message queue through the synchronous calling thread or the asynchronous calling thread by using the mutual exclusion mechanism.
13. A message processing method, comprising:
acquiring 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, and the message queue is constructed by a callback function corresponding to user service;
calling the callback function through the working thread to process the first message;
acquiring a second message from the head of the message queue through the working thread;
wherein the method further comprises: obtaining, by the worker thread, the first message or the second message from a head of the message queue comprises: acquiring the first message or the second message from the head of a message queue through the working thread by using a mutual exclusion mechanism; adding a new message at the tail of the message queue through a synchronous calling thread or an asynchronous calling thread comprises the following steps: and adding a new message at the tail part of the message queue through the synchronous calling thread or the asynchronous calling thread by using a mutual exclusion mechanism.
CN201810463425.4A 2018-05-15 2018-05-15 Message processing method and device Active CN108717380B (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN201810463425.4A CN108717380B (en) 2018-05-15 2018-05-15 Message processing method and device
CN202110882353.9A CN113656189A (en) 2018-05-15 2018-05-15 Message processing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810463425.4A CN108717380B (en) 2018-05-15 2018-05-15 Message processing method and device

Related Child Applications (1)

Application Number Title Priority Date Filing Date
CN202110882353.9A Division CN113656189A (en) 2018-05-15 2018-05-15 Message processing method and device

Publications (2)

Publication Number Publication Date
CN108717380A CN108717380A (en) 2018-10-30
CN108717380B true CN108717380B (en) 2021-08-10

Family

ID=63899987

Family Applications (2)

Application Number Title Priority Date Filing Date
CN201810463425.4A Active CN108717380B (en) 2018-05-15 2018-05-15 Message processing method and device
CN202110882353.9A Pending CN113656189A (en) 2018-05-15 2018-05-15 Message processing method and device

Family Applications After (1)

Application Number Title Priority Date Filing Date
CN202110882353.9A Pending CN113656189A (en) 2018-05-15 2018-05-15 Message processing method and device

Country Status (1)

Country Link
CN (2) CN108717380B (en)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109918209B (en) * 2019-01-28 2021-02-02 深兰科技(上海)有限公司 Method and equipment for communication between threads
CN111796930B (en) * 2020-05-21 2023-12-22 福建天泉教育科技有限公司 Method for optimizing message processing and storage medium
CN114338806B (en) * 2022-02-28 2022-06-21 湖南云畅网络科技有限公司 Synchronous message processing method and system
CN117472397B (en) * 2023-12-27 2024-04-05 柏科数据技术(深圳)股份有限公司 Data mirror image control method, device, terminal and storage medium

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106161558A (en) * 2015-04-21 2016-11-23 阿里巴巴集团控股有限公司 A kind of request processing method and device

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106161558A (en) * 2015-04-21 2016-11-23 阿里巴巴集团控股有限公司 A kind of request processing method and device

Also Published As

Publication number Publication date
CN108717380A (en) 2018-10-30
CN113656189A (en) 2021-11-16

Similar Documents

Publication Publication Date Title
CN108717380B (en) Message processing method and device
TWI267782B (en) Deallocation of computer data in a multithreaded computer
JP3807588B2 (en) Multi-thread processing apparatus, processing method, and computer-readable recording medium storing multi-thread program
CN107479981B (en) Processing method and device for realizing synchronous call based on asynchronous call
US9052972B2 (en) Determining the processing order of a plurality of events
CN106383764B (en) A kind of collecting method and equipment
CN110765288B (en) Image information synchronization method, device and system and storage medium
US20140053157A1 (en) Asynchronous execution flow
CN105094811B (en) A kind of method and apparatus of event handling
CN106502770B (en) HMI state transition method based on finite state machine
CN111221630A (en) Business process processing method, device, equipment, readable storage medium and system
CN111274019A (en) Data processing method and device and computer readable storage medium
CN110784377A (en) Method for uniformly managing cloud monitoring data in multi-cloud environment
CN114153584A (en) Task processing method, equipment and storage medium for game frame circulation
CN111737275A (en) Database update event processing method and device and computer readable storage medium
CN111338769A (en) Data processing method and device and computer readable storage medium
CN105871960B (en) Method, device and equipment for calling data sharing
TW200905567A (en) Notifying user mode scheduler of blocking events
CN111310638A (en) Data processing method and device and computer readable storage medium
CN109240773B (en) Method, device, terminal and readable storage medium for solving room string data
CN106598726A (en) Multi-task management system and distributed deployment method thereof
CN110083357B (en) Interface construction method, device, server and storage medium
CN111506644B (en) Application data processing method and device and electronic equipment
CN110262912B (en) Calling method and device for procedure call gRPC
CN107180107B (en) Method and system for uploading selected files by mobile phone attachment under Ionic framework

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant