Inter-thread communication method, device, electronic device and storage medium
By detecting the status of child thread tasks in the main thread and obtaining processing results, the problem of long inter-thread communication time is solved, and more efficient inter-thread communication is achieved.
Patent Information
- Application Number
- CN202110347292.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-31
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2041-03-31
AI Technical Summary
In the prior art, the communication time between threads is long, mainly because the main thread needs to wait for the child thread task processing results. If the main thread has more tasks, the waiting time will be long.
When executing the main thread task, it detects whether the corresponding child thread of the main thread completes the child thread task. If the child thread has completed, get its processing result. If the corresponding processing result task is executed, the main thread task is executed. By adding task detection instructions to the task processing instructions, the main thread can detect the task status of the child thread in time and directly obtain the processing results after the child thread completes the task.
It reduces communication time between threads, improves communication efficiency between threads, and avoids long-term waiting caused by waiting for the main thread task to complete.
Smart Images

Figure CN113064739B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of communication technology, and in particular to an inter-thread communication method, device, electronic device and storage medium. Background Art
[0002] With the development of computer technology, more and more tasks are executed through threads; for example, some interactive tasks are executed through the main thread, and some time-consuming tasks are executed through the child thread.
[0003] In the related art, the current inter-thread communication method is generally that after the child thread completes the time-consuming task, it returns the corresponding task processing result to the main thread. If the main thread still has tasks that have not been completed at this time, the task processing result of the child thread needs to wait until the main thread's task is completed before it can be returned to the main thread; however, if the main thread has many tasks, the waiting time will be very long, resulting in a longer communication time between threads. Summary of the invention
[0004] The present disclosure provides a method, device, electronic device and storage medium for inter-thread communication, so as to at least solve the problem of long inter-thread communication time in the related art. The technical solution of the present disclosure is as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, there is provided an inter-thread communication method, comprising:
[0006] When executing the main thread task, detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task;
[0007] If the sub-thread completes the sub-thread task, obtaining the processing result of the sub-thread task;
[0008] If the task corresponding to the processing result is completed, the main thread task is executed.
[0009] In an exemplary embodiment, when executing the main thread task, before detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task, the method further includes:
[0010] Obtain a task processing instruction for executing the main thread task;
[0011] A task detection instruction is added to the task processing instruction; the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
[0012] In an exemplary embodiment, the obtaining of a task processing instruction for executing the main thread task includes:
[0013] Obtaining the task processing mode of the main thread task;
[0014] Obtaining a task processing function corresponding to the task processing mode as the task processing function corresponding to the main thread task;
[0015] The task processing function corresponding to the main thread task is identified as a task processing instruction for executing the main thread task.
[0016] In an exemplary embodiment, when executing the main thread task, detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task includes:
[0017] When the task processing instruction is called to execute the main thread task, if it is recognized that the task detection instruction is added to the task processing instruction, the task detection instruction is used to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task.
[0018] In an exemplary embodiment, the detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task includes:
[0019] Obtain the semaphore corresponding to the sub-thread task;
[0020] If the semaphore is a target semaphore, it is confirmed that the sub-thread has completed the sub-thread task; the target semaphore is used to indicate that the sub-thread task has been completed.
[0021] In an exemplary embodiment, after the processing result is completed, the main thread task is executed, and the following is further included:
[0022] If all sub-thread tasks in the sub-thread task table are executed, the task detection instruction in the task processing instruction is deleted.
[0023] In an exemplary embodiment, after obtaining the processing result of the sub-thread task, the method further includes:
[0024] Creating a task corresponding to the processing result; the priority of the task corresponding to the processing result is higher than the priority of the main thread task;
[0025] The task corresponding to the processing result is processed.
[0026] In an exemplary embodiment, when executing the main thread task, after detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task, the method further includes:
[0027] If the sub-thread has not completed the sub-thread task, determining the remaining completion time of the sub-thread task;
[0028] If the remaining completion time is less than the preset waiting time, the sub-thread task is monitored.
[0029] In an exemplary embodiment, determining the remaining completion time of the sub-thread task includes:
[0030] Obtain the remaining tasks of the sub-thread task;
[0031] The remaining tasks of the sub-thread task are input into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
[0032] According to a second aspect of an embodiment of the present disclosure, there is provided an inter-thread communication device, which is applied to a main thread and includes:
[0033] The task detection unit is configured to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task when executing the main thread task;
[0034] A result acquisition unit is configured to acquire a processing result of the sub-thread task if the sub-thread completes the sub-thread task;
[0035] The task execution unit is configured to execute the main thread task if the task corresponding to the processing result is completed.
[0036] In an exemplary embodiment, the device also includes an instruction adding unit, which is configured to execute a task processing instruction for obtaining and executing the main thread task; add a task detection instruction in the task processing instruction; and the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
[0037] In an exemplary embodiment, the instruction adding unit is also configured to execute the task processing method of obtaining the main thread task; obtaining the task processing function corresponding to the task processing method as the task processing function corresponding to the main thread task; and identifying the task processing function corresponding to the main thread task as a task processing instruction for executing the main thread task.
[0038] In an exemplary embodiment, the task detection unit is further configured to execute, when the task processing instruction is called to execute the main thread task, if it is recognized that the task detection instruction is added to the task processing instruction, then through the task detection instruction, detect whether the sub-thread corresponding to the main thread has completed the sub-thread task.
[0039] In an exemplary embodiment, the task detection unit is further configured to execute acquisition of a semaphore corresponding to the sub-thread task; if the semaphore is a target semaphore, it confirms that the sub-thread has completed the execution of the sub-thread task; the target semaphore is used to indicate that the execution of the sub-thread task is completed.
[0040] In an exemplary embodiment, the apparatus further comprises an instruction deleting unit configured to delete the task detection instruction in the task processing instruction if all sub-thread tasks in the sub-thread task table are executed.
[0041] In an exemplary embodiment, the device also includes a task creation unit configured to execute creation of a task corresponding to the processing result; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; and the task corresponding to the processing result is processed.
[0042] In an exemplary embodiment, the device also includes a task monitoring unit, which is configured to determine the remaining completion time of the sub-thread task if the sub-thread has not completed the sub-thread task; if the remaining completion time is less than a preset waiting time, monitor the sub-thread task.
[0043] In an exemplary embodiment, the task monitoring unit is further configured to execute the acquisition of the remaining tasks of the sub-thread task; input the remaining tasks of the sub-thread task into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
[0044] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the inter-thread communication method as described in any one of the embodiments of the first aspect.
[0045] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided, comprising: when instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the inter-thread communication method described in any one of the embodiments of the first aspect.
[0046] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, the computer program product comprising a computer program, the computer program being stored in a computer-readable storage medium, at least one processor of a device reading and executing the computer program from the computer-readable storage medium, so that the device executes the inter-thread communication method described in any one of the embodiments of the first aspect.
[0047] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0048] When executing the main thread task, check whether the child thread corresponding to the main thread has completed the child thread task; if the child thread has completed the child thread task, obtain the processing result of the child thread task; if the task corresponding to the processing result has been completed, execute the main thread task; in this way, before executing the main thread task, first check whether the child thread corresponding to the main thread has completed the child thread task, and after the child thread has completed the child thread task, obtain the processing result of the child thread task, and process the task corresponding to the processing result, and then process the main thread task; avoid the defect that the processing result of the child thread task needs to wait for the main thread to complete processing all the main thread tasks before returning to the main thread, resulting in a long communication time between threads; thereby reducing the communication time between threads and further improving the communication efficiency between threads.
[0049] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0051] Figure 1 The present invention is an application environment diagram of an inter-thread communication method according to an exemplary embodiment.
[0052] Figure 2 The present invention is a flowchart showing an inter-thread communication method according to an exemplary embodiment.
[0053] Figure 3 The present invention is a flowchart showing the steps of adding a task detection instruction to a task processing instruction for executing a main thread task according to an exemplary embodiment.
[0054] Figure 4 The invention is a flowchart showing another inter-thread communication method according to an exemplary embodiment.
[0055] Figure 5 The flowchart of another inter-thread communication method is shown according to an exemplary embodiment.
[0056] Figure 6 The invention is a block diagram showing an inter-thread communication device according to an exemplary embodiment.
[0057] Figure 7It is a block diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0058] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0059] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure 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 disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0060] The inter-thread communication method provided by the present disclosure can be applied to Figure 1 In the application environment shown in the figure, Figure 1 , the application environment diagram includes a main thread 110 and a sub-thread 120, and the main thread 110 and the sub-thread 120 can communicate with each other; specifically, refer to Figure 1 When executing the main thread task, the main thread 110 detects whether the sub-thread 120 corresponding to the main thread 110 has completed the sub-thread task; if the sub-thread 120 has completed the sub-thread task, the processing result of the sub-thread task is obtained; if the task corresponding to the processing result is completed, the main thread task is executed. It should be noted that the present disclosure is mainly applied in the iOS system.
[0061] Figure 2 is a flowchart of an inter-thread communication method according to an exemplary embodiment. Figure 2 As shown, the inter-thread communication method is used as follows Figure 1 The main thread shown includes the following steps:
[0062] In step S210, when the main thread task is executed, it is detected whether the sub-thread corresponding to the main thread has completed the sub-thread task.
[0063] Among them, a thread refers to the smallest unit that an operating system (such as the iOS operating system) can perform calculation scheduling. It is contained in a process. Multiple threads can run concurrently in a process, and each thread executes different tasks in parallel.
[0064] The main thread is the thread used to perform UI (User Interface) operation tasks. All operation tasks related to user display and interaction are performed in the main thread. The sub-thread is the thread used to perform time-consuming operation tasks unrelated to UI operation (i.e. non-UI operation). The main thread task is the UI operation task, and the sub-thread task is the time-consuming operation task unrelated to UI operation. It should be noted that one main thread can correspond to multiple sub-threads.
[0065] Specifically, when the main thread executes the main thread task, the main thread detects whether the sub-thread corresponding to the main thread has completed the sub-thread task through the task detection instruction; the task detection instruction is an instruction used to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task. In this way, before the main thread executes the main thread task, it is first determined whether the sub-thread corresponding to the main thread has completed the sub-thread task, so that after the sub-thread completes the sub-thread task, the processing result of the sub-thread task can be directly obtained, without waiting for the main thread to complete all the sub-thread tasks before returning the processing result of the sub-thread task to the main thread.
[0066] For example, in the iOS system, when a time-consuming operation task unrelated to UI operation occurs, the iOS system will create a child thread and treat the time-consuming operation task unrelated to UI operation as a child thread task and switch to the child thread for execution; when the child thread completes the child thread task, the processing result of the child thread task will be saved to wait for the main thread to detect and return it to the main thread for execution. Next, before executing the main thread task, the main thread will detect whether the child thread has completed the child thread task.
[0067] In step S220, if the child thread completes the child thread task, the processing result of the child thread task is obtained.
[0068] Among them, the processing result of the sub-thread task can refer to a simple numerical value or a code block, and a simple numerical value can be encapsulated into a code block, so the processing result of the sub-thread task is passed to the main thread in the form of a code block.
[0069] It should be noted that after the sub-thread completes the sub-thread task, the sub-thread can be deleted to reduce the additional performance overhead of the system.
[0070] In step S230, if the task corresponding to the processing result is completed, the main thread task is executed.
[0071] Among them, the task corresponding to the processing result refers to the task related to the processing result of the sub-thread task and needs to be executed in the main thread, and the present disclosure does not limit it specifically.
[0072] Specifically, when it is detected that the child thread has completed the child thread task, the processing result of the child thread task is obtained through the main thread, and then a task corresponding to the processing result of the child thread task is created as a task before the main thread task; the task corresponding to the processing result of the child thread task is processed, and when the task corresponding to the processing result of the child thread task is processed, the main thread task is processed. In this way, after the child thread completes the child thread task, the processing result of the child thread task is directly obtained, and after the task corresponding to the processing result of the child thread task is executed, the main thread task is executed, which is conducive to avoiding the defect that the processing result of the child thread task needs to wait until the main thread completes processing all the main thread tasks before returning to the main thread, resulting in a long communication time between threads. It ensures that the processing result of the child thread task can be quickly returned to the main thread, thereby reducing the communication time between threads and further improving the communication efficiency between threads.
[0073] In the above-mentioned inter-thread communication method, when executing the main thread task, it is detected whether the child thread corresponding to the main thread has completed the child thread task; if the child thread has completed the child thread task, the processing result of the child thread task is obtained; if the task corresponding to the processing result is completed, the main thread task is executed; in this way, before executing the main thread task, it is first detected whether the child thread corresponding to the main thread has completed the child thread task, and after the child thread has completed the child thread task, the processing result of the child thread task is obtained, and the task corresponding to the processing result is processed, and then the main thread task is processed; this avoids the defect that the processing result of the child thread task needs to wait until the main thread has processed all the main thread tasks before returning to the main thread, resulting in a long communication time between threads; thereby reducing the communication time between threads and further improving the communication efficiency between threads.
[0074] In an exemplary embodiment, if Figure 3 As shown, in step S210, when executing the main thread task, before detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task, it also includes the step of adding a task detection instruction in the task processing instruction for executing the main thread task, which specifically includes the following contents:
[0075] In step S310, a task processing instruction for executing the main thread task is obtained.
[0076] Among them, the task processing instruction for executing the main thread task refers to the OC method for executing the main thread task, specifically refers to the msgSend function called by the execution of the main thread task; the msgSend function is a function that will be called when each OC method is executed, and how the OC method is executed is determined by the msgsend function; the OC method is the programming language developed by iOS APP (Application), and the msgSend function is the corresponding underlying implementation.
[0077] Specifically, the main thread obtains the task processing method of the main thread task; obtains the task processing function corresponding to the task processing method as the task processing function corresponding to the main thread task; and identifies the task processing function corresponding to the main thread task as a task processing instruction for executing the main thread task. The task processing method refers to the method for completing the main thread task, and the task processing function refers to the function called by executing the main thread task, such as the msgSend function.
[0078] For example, the main thread identifies the main thread task and obtains the task processing method of the main thread task, such as how the main thread task is implemented; then it queries the correspondence between the preset task processing method and the task processing function, and obtains the task processing function corresponding to the task processing method of the main thread task as the task processing function corresponding to the main thread task, such as obtaining the msgSend function corresponding to the task processing method; finally, the task processing function corresponding to the main thread task is used as the task processing instruction to execute the main thread task.
[0079] In step S320, a task detection instruction is added to the task processing instruction; the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
[0080] The task processing instruction with the task detection instruction added thereto is used to detect whether the child thread has completed the child thread task before executing the main thread task. In actual scenarios, adding the task detection instruction to the task processing instruction means hooking the msgSend function that executes the main thread task; before calling the msgSend function after the hook, it will be detected whether the child thread has completed the child thread task.
[0081] Specifically, the main thread adds a task detection instruction for detecting whether the sub-thread has completed the sub-thread task to a corresponding position in the task processing instruction for executing the main thread task, thereby obtaining a task processing instruction with the task detection instruction added thereto.
[0082] For example, fishhook is used to hook the msgSend function that executes the main thread task; after the msgSend function that executes the main thread task is hooked, it is possible to check whether any sub-thread task has been completed before each msgSend function is executed.
[0083] The technical solution provided by the embodiment of the present disclosure obtains a task processing instruction for executing the main thread task, and adds a task detection instruction for detecting whether the sub-thread has completed the sub-thread task in the task processing instruction. When the task processing instruction is called to execute the main thread task, it is detected whether the sub-thread has completed the sub-thread task. This is beneficial to ensure that the processing result of the sub-thread task is quickly returned to the main thread, thereby reducing the communication time between the main thread and the sub-thread.
[0084] In an exemplary embodiment, in step S210, when executing the main thread task, it is detected whether the sub-thread corresponding to the main thread has completed the sub-thread task, including: when calling the task processing instruction to execute the main thread task, if it is recognized that a task detection instruction is added to the task processing instruction, then through the task detection instruction, it is detected whether the sub-thread corresponding to the main thread has completed the sub-thread task.
[0085] Specifically, when the main thread calls the task processing instruction to execute the main thread task, it is detected whether the task processing instruction is added with a task detection instruction for detecting whether the child thread has completed the child thread task. If it is detected that the task processing instruction is added with a task detection instruction, the task processing status of the child thread task is obtained through the task detection instruction; according to the task processing status of the child thread task, it is determined whether the child thread has completed the child thread task. For example, if the task processing status of the child thread task is in the processing state, it means that the child thread has not completed the child thread task; if the task processing status of the child thread task is in the processing state, it means that the child thread has completed the child thread task.
[0086] For example, when the main thread calls the msgSend function to execute the main thread task, if the msgSend function is a hooked msgSend function, it is detected whether the child thread corresponding to the main thread has completed the child thread task.
[0087] The technical solution provided by the embodiment of the present disclosure, when calling the task processing instruction to execute the main thread task, detects whether the sub-thread corresponding to the main thread has completed the sub-thread task through the task detection instruction in the task processing instruction. This is beneficial for obtaining the processing result of the sub-thread task when the sub-thread completes the sub-thread task. There is no need to wait for the main thread task to be completed before obtaining the processing result of the sub-thread task, which further reduces the communication time between the main thread and the sub-thread.
[0088] In an exemplary embodiment, in step S210, it is detected whether the sub-thread corresponding to the main thread has completed the sub-thread task, including: obtaining a semaphore corresponding to the sub-thread task; if the semaphore is a target semaphore, confirming that the sub-thread has completed the sub-thread task; the target semaphore is used to indicate that the sub-thread task is completed.
[0089] Among them, in the iOS system, the task processing status of the child thread task is generally detected through the semaphore lock provided by Apple; the semaphore lock is thread-safe by default, and it has two values, 0 and 1. When the semaphore value is 0, it means that the child thread task has not been completed. When the semaphore value is 1, it means that the child thread task has been completed.
[0090] It should be noted that the target semaphore is a semaphore with an index value of 1; when the child thread completes the child thread task, the semaphore corresponding to the child thread task is marked as the target semaphore.
[0091] Specifically, the main thread obtains the semaphore corresponding to the child thread task, and compares the semaphore corresponding to the child thread task with the target semaphore. If the semaphore corresponding to the child thread task belongs to the target semaphore, it means that the task processing status of the child thread task is the processing completion status, indicating that the child thread has completed the sub-thread task; if the semaphore corresponding to the child thread task does not belong to the target semaphore, it means that the task processing status of the child thread task is the processing status, indicating that the child thread has not completed the sub-thread task.
[0092] The technical solution provided by the embodiment of the present disclosure obtains the semaphore corresponding to the sub-thread task, and when the semaphore corresponding to the sub-thread task is the target semaphore, it confirms that the sub-thread has completed the sub-thread task. In this way, the task processing status of the sub-thread task is detected by the semaphore, which makes the determination of the task processing status of the sub-thread task more accurate, thereby improving the accuracy of determining the task processing status of the sub-thread task.
[0093] In an exemplary embodiment, in step S230, after executing the main thread task if the processing result is completed, the method further includes: if all sub-thread tasks in the sub-thread task table are completed, deleting the task detection instruction in the task processing instruction.
[0094] Among them, in the actual scenario, deleting the task detection instruction in the task processing instruction means releasing the hook of the msgSend function.
[0095] Specifically, in a scenario where a sub-thread task table includes multiple sub-thread tasks, after the main thread executes the main thread task, the task processing status of the sub-thread tasks in the sub-thread task table is detected. If the task processing status of the sub-thread tasks in the sub-thread task table are all in the processing completion status, it is confirmed that the sub-thread tasks in the sub-thread task table are all executed, and then the task detection instruction in the task processing instruction is deleted.
[0096] For example, after the main thread completes the main thread task, release the hook of the msgSend function, and then all OC methods will use the iOS system's default msgSend function.
[0097] It should be noted that since all the sub-thread tasks in the sub-thread task table have been completed, it means that there is no need to detect whether the sub-thread tasks are completed, that is, there is no need to perform inter-thread communication, so the hook for the msgsend function is released to avoid causing additional performance overhead for the iOS system.
[0098] The technical solution provided by the embodiment of the present disclosure is that if all sub-thread tasks in the sub-thread task table are completed, the task detection instruction in the task processing instruction is deleted, ensuring that when the main thread task is subsequently executed, there is no need to detect whether the sub-thread task is completed, thereby reducing the system's additional performance overhead and ensuring the system's task processing efficiency.
[0099] In an exemplary embodiment, in step S220, after obtaining the processing result of the sub-thread task, it also includes: creating a task corresponding to the processing result; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; and processing the task corresponding to the processing result.
[0100] Among them, the priority of the task corresponding to the processing result is higher than the priority of the main thread task, which means that the task corresponding to the processing result is executed first, and then the main thread task is executed; in this way, it is equivalent to inserting a new task into the task queue originally to be executed.
[0101] Specifically, the main thread creates a task corresponding to the processing result, and determines the priority of the task corresponding to the processing result, so that the priority of the task corresponding to the processing result is higher than the priority of the main thread task, that is, the task corresponding to the processing result is placed before the main thread task; the task corresponding to the processing result is processed, and if the task corresponding to the processing result is completed, the main thread task is processed.
[0102] For example, when a sub-thread task is completed, the processing result of the sub-thread task is encapsulated into a code block, the main thread obtains the code block, and executes the task corresponding to the code block; if the task corresponding to the code block is completed, the next task originally to be executed in the main thread is resumed.
[0103] The technical solution provided by the embodiment of the present disclosure helps to reduce the communication time between threads by creating tasks corresponding to the processing results, and first executing the tasks corresponding to the processing results, and then executing the main thread tasks, thereby improving the communication efficiency between threads.
[0104] In an exemplary embodiment, in step S210, when executing the main thread task, after detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task, it also includes: if the sub-thread has not completed the sub-thread task, determining the remaining completion time of the sub-thread task; if the remaining completion time is less than the preset waiting time, monitoring the sub-thread task.
[0105] The remaining completion time of the sub-thread task refers to the remaining time required to complete the sub-thread task. The preset waiting time refers to the allowed consumption time; for example, the sub-thread task will be completed in 2 seconds. If a new main thread task is executed at this time, and the processing time of the main thread task is longer, such as 1 minute, then the processing result of the sub-thread task needs to wait until the main thread task is completed before returning to the main thread; in this case, waiting for the sub-thread task to be completed, then processing the task corresponding to the processing result of the sub-thread task, and finally processing the main thread task, is conducive to reducing the communication time between threads.
[0106] Specifically, if the sub-thread has not completed the sub-thread task, the remaining completion time of the sub-thread task is determined through a pre-trained time prediction model; if the remaining completion time of the sub-thread task is less than the preset waiting time, the sub-thread task is monitored; if the sub-thread task is monitored to be completed, the processing result of the sub-thread task is obtained; a task corresponding to the processing result of the sub-thread task is created, and the task corresponding to the processing result of the sub-thread task is processed; if the task corresponding to the processing result of the sub-thread task is completed, the main thread task is processed. Among them, the pre-trained time prediction model is a deep learning model for predicting the remaining completion time of the sub-thread task, which is obtained after repeated training.
[0107] Furthermore, when determining the remaining completion time of the sub-thread task, the main thread obtains the remaining tasks of the sub-thread task; the remaining tasks of the sub-thread task are input into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
[0108] For example, the main thread obtains instructions based on the remaining tasks to obtain the remaining tasks of the sub-thread tasks; then the remaining tasks of the sub-thread tasks are input into a pre-trained time prediction model, and the remaining tasks of the sub-thread tasks are analyzed through the pre-trained time prediction model to obtain the remaining completion time of the sub-thread tasks as the remaining completion time of the sub-thread tasks.
[0109] The technical solution provided by the embodiment of the present disclosure monitors the sub-thread task if the sub-thread has not completed the sub-thread task and the remaining completion time of the sub-thread task is less than the preset waiting time. This is beneficial for obtaining the processing result of the sub-thread task when the sub-thread task is monitored to be completed, and processing the task corresponding to the processing result of the sub-thread task, thereby avoiding the defect of processing the main thread task when the sub-thread task is about to be completed, resulting in a long waiting time for the processing result of the sub-thread task and a long communication time between threads, thereby further reducing the communication time between threads.
[0110] Figure 4 is a flowchart of another inter-thread communication method according to an exemplary embodiment. Figure 4 As shown, the inter-thread communication method is used as follows Figure 1 The main thread shown includes the following steps:
[0111] In step S410, a task processing instruction for executing the main thread task is obtained; a task detection instruction is added to the task processing instruction; the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
[0112] In step S420, when the task processing instruction is called to execute the main thread task, if it is recognized that a task detection instruction is added to the task processing instruction, the semaphore corresponding to the sub-thread task is obtained through the task detection instruction.
[0113] In step S430, if the semaphore is the target semaphore, it is confirmed that the child thread has completed the execution of the child thread task; the target semaphore is used to indicate that the execution of the child thread task is completed.
[0114] In step S440, the processing result of the sub-thread task is obtained, and a task corresponding to the processing result is created; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; and the task corresponding to the processing result is processed.
[0115] In step S450, if the task corresponding to the processing result is completed, the main thread task is executed.
[0116] In step S460, if the main thread task is executed completely and all the sub-thread tasks in the sub-thread task table are executed completely, the task detection instruction in the task processing instruction is deleted.
[0117] In the above-mentioned inter-thread communication method, before executing the main thread task, it is first detected whether the child thread corresponding to the main thread has completed the child thread task, and after the child thread has completed the child thread task, the processing result of the child thread task is obtained, and the task corresponding to the processing result is processed, and then the main thread task is processed; this avoids the defect that the processing result of the sub-thread task needs to wait until the main thread has processed all the main thread tasks before returning to the main thread, resulting in a long communication time between threads; thereby reducing the communication time between threads and further improving the communication efficiency between threads.
[0118] In order to more clearly illustrate the inter-thread communication method provided by the embodiment of the present disclosure, the inter-thread communication method is specifically described below with a specific embodiment. Figure 5 As shown, the present disclosure also provides an inter-thread communication method, which specifically includes the following contents:
[0119] Hook the msgSend function of the main thread through fishhook. msgSend is a function that all OC methods will eventually execute. After hooking the msgSend function of the main thread, you can check whether the tasks registered by the child thread have been completed before each msgSend function is executed. The way to detect and register the child thread task is through the dispatchSemphore semaphore. This semaphore is thread-safe and can ensure that there will be no thread read and write errors during the detection and registration process. The timing of registering the child thread signal is before the child thread task is executed. In addition, the code block that needs to be returned to the main thread for execution after the child thread task is completed will be saved through block. When the time-consuming task of the child thread is completed, the registered task can wait to be detected by the main thread and executed. Finally, release the hook of the msgSend function after the execution is completed.
[0120] For example, see Figure 5, before switching from the main thread to the child thread, hook the msgSend function of the main thread; thereafter, all OC methods will go through the msgSend function after the hook; before each msgSend function is executed, a semaphore is used to detect whether there is a registered child thread task that has been completed; before the execution of the time-consuming task of the child thread, the task will be registered; after the time-consuming task is completed, the task will be marked as completed through a semaphore, waiting to be detected by the main thread and returned to the main thread for execution; before a certain msgSend function is executed, it is detected that the task of the child thread has been completed, and the asynchronous callback method that needs to be executed in the main thread is directly "inserted"; after completion, the native msgSend function is executed; after the main thread task is completed, the hook of the msgSend function is released, and thereafter all OC methods will go through the system default msgSend function.
[0121] It should be noted that Figure 5 In the command, mainThread refers to the main thread, subThread refers to the child thread, method refers to the thread switching method, GCD refers to a thread switching scheme, original msgSend refers to the msgSend function originally called by the main thread; result refers to the task processing result of the child thread; hooking the main thread msgSend method means adding a task detection instruction to the msgSend function called by the main thread; unhooking the main thread msgSend method means deleting the task detection instruction in the msgSend function called by the main thread.
[0122] The above-mentioned inter-thread communication method can achieve the following technical effects: (1) solve the problem that the inter-thread communication time is too long when the main thread has too many tasks; (2) improve the startup speed of iOS applications, allowing users to start the APP more quickly.
[0123] It should be understood that although Figure 2-4 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 2-4 At least part of the steps may include multiple steps or multiple stages. These steps or stages are not necessarily performed at the same time, but can be performed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed in turn or alternately with other steps or at least part of the steps or stages in other steps.
[0124] Figure 6is a block diagram of an inter-thread communication device according to an exemplary embodiment. Figure 6 The device includes a task detection unit 610, a result acquisition unit 620 and a task execution unit 630.
[0125] The task detection unit 610 is configured to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task when executing the main thread task.
[0126] The result acquisition unit 620 is configured to acquire the processing result of the sub-thread task if the sub-thread completes the sub-thread task.
[0127] The task execution unit 630 is configured to execute the main thread task if the task corresponding to the processing result is completed.
[0128] In an exemplary embodiment, the inter-thread communication device provided by the present disclosure also includes an instruction adding unit, which is configured to execute a task processing instruction for obtaining a task for executing a main thread task; adding a task detection instruction in the task processing instruction; and the task detection instruction is used to detect whether a child thread has completed the execution of the child thread task.
[0129] In an exemplary embodiment, the instruction adding unit is also configured to execute a task processing method for obtaining the main thread task; obtain a task processing function corresponding to the task processing method as the task processing function corresponding to the main thread task; and identify the task processing function corresponding to the main thread task as a task processing instruction for executing the main thread task.
[0130] In an exemplary embodiment, the task detection unit 610 is further configured to execute, when calling a task processing instruction to execute a main thread task, if it is recognized that a task detection instruction is added to the task processing instruction, then through the task detection instruction, detect whether the sub-thread corresponding to the main thread has completed the sub-thread task.
[0131] In an exemplary embodiment, the task detection unit 610 is further configured to execute acquisition of a semaphore corresponding to the sub-thread task; if the semaphore is a target semaphore, it is confirmed that the sub-thread has completed the sub-thread task; the target semaphore is used to indicate that the sub-thread task is completed.
[0132] In an exemplary embodiment, the inter-thread communication device provided by the present disclosure further includes an instruction deleting unit configured to delete a task detection instruction in a task processing instruction if all sub-thread tasks in the sub-thread task table are executed.
[0133] In an exemplary embodiment, the inter-thread communication device provided by the present disclosure also includes a task creation unit, which is configured to execute the creation of a task corresponding to the processing result; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; and the task corresponding to the processing result is processed.
[0134] In an exemplary embodiment, the inter-thread communication device provided by the present disclosure also includes a task monitoring unit, which is configured to determine the remaining completion time of the sub-thread task if the sub-thread has not completed the sub-thread task; if the remaining completion time is less than the preset waiting time, the sub-thread task is monitored.
[0135] In an exemplary embodiment, the task monitoring unit is further configured to execute the acquisition of the remaining tasks of the sub-thread task; input the remaining tasks of the sub-thread task into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
[0136] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0137] Figure 7 7 is a block diagram of a device 700 for executing the above-mentioned inter-thread communication method according to an exemplary embodiment. For example, the device 700 may be a mobile phone, a computer, a digital broadcast terminal, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
[0138] Reference Figure 7 , device 700 may include one or more of the following components: a processing component 702 , a memory 704 , a power component 706 , a multimedia component 708 , an audio component 710 , an input / output (I / O) interface 712 , a sensor component 714 , and a communication component 716 .
[0139] The processing component 702 generally controls the overall operation of the device 700, such as operations associated with display, phone calls, data communications, camera operations, and recording operations. The processing component 702 may include one or more processors 720 to execute instructions to complete all or part of the steps of the above-mentioned method. In addition, the processing component 702 may include one or more modules to facilitate the interaction between the processing component 702 and other components. For example, the processing component 702 may include a multimedia module to facilitate the interaction between the multimedia component 708 and the processing component 702.
[0140] The memory 704 is configured to store various types of data to support operations on the device 700. Examples of such data include instructions for any application or method operating on the device 700, contact data, phone book data, messages, pictures, videos, etc. The memory 704 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.
[0141] The power supply component 706 provides power to the various components of the device 700. The power supply component 706 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the device 700.
[0142] The multimedia component 708 includes a screen that provides an output interface between the device 700 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundaries of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 708 includes a front camera and / or a rear camera. When the device 700 is in an operating mode, such as a shooting mode or a video mode, the front camera and / or the rear camera may receive external multimedia data. Each front camera and rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
[0143] The audio component 710 is configured to output and / or input audio signals. For example, the audio component 710 includes a microphone (MIC), and when the device 700 is in an operating mode, such as a call mode, a recording mode, and a speech recognition mode, the microphone is configured to receive an external audio signal. The received audio signal can be further stored in the memory 704 or sent via the communication component 716. In some embodiments, the audio component 710 also includes a speaker for outputting audio signals.
[0144] I / O interface 712 provides an interface between processing component 702 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include but are not limited to: a home button, a volume button, a start button, and a lock button.
[0145] The sensor assembly 714 includes one or more sensors for providing various aspects of status assessment for the device 700. For example, the sensor assembly 714 can detect the open / closed state of the device 700, the relative positioning of components, such as the display and keypad of the device 700, and the sensor assembly 714 can also detect the position change of the device 700 or a component of the device 700, the presence or absence of user contact with the device 700, the orientation or acceleration / deceleration of the device 700, and the temperature change of the device 700. The sensor assembly 714 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. The sensor assembly 714 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 714 may also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
[0146] The communication component 716 is configured to facilitate wired or wireless communication between the device 700 and other devices. The device 700 can access a wireless network based on a communication standard, such as WiFi, a carrier network (such as 2G, 3G, 4G or 5G), or a combination thereof. In an exemplary embodiment, the communication component 716 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 716 also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology and other technologies.
[0147] In an exemplary embodiment, the device 700 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the above methods.
[0148] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory 704 including instructions, which can be executed by a processor 720 of the device 700 to perform the above method. For example, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0149] In an exemplary embodiment, a computer program product is also provided, which includes a computer program stored in a computer-readable storage medium, and at least one processor of a device reads and executes the computer program from the computer-readable storage medium, so that the device executes the inter-thread communication method in any embodiment of the present disclosure.
[0150] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0151] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A method for inter-thread communication, characterized in that: Applied to the main thread, including: When executing the main thread task, by executing the task detection instruction in the task processing instruction of the main thread task, it is detected whether the sub-thread corresponding to the main thread has completed the sub-thread task; If the sub-thread completes the sub-thread task, the processing result of the sub-thread task is obtained; if the sub-thread does not complete the sub-thread task, and the remaining completion time of the sub-thread task is less than the preset waiting time, the sub-thread task is monitored, and if it is monitored that the sub-thread task is completed, the processing result of the sub-thread task is obtained; Creating a task corresponding to the processing result, and processing the task corresponding to the processing result; the task corresponding to the processing result is used to represent a task related to the processing result and needs to be executed in the main thread; If the task corresponding to the processing result is completed, the main thread task is executed; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; The detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task includes: Obtain the semaphore corresponding to the sub-thread task; If the semaphore is a target semaphore, it is confirmed that the sub-thread has completed the sub-thread task; the target semaphore is used to indicate that the sub-thread task has been completed.
2. The inter-thread communication method according to claim 1, characterized in that: When executing the main thread task, before detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task by executing the task detection instruction in the task processing instruction of the main thread task, the method further includes: Obtain a task processing instruction for executing the main thread task; A task detection instruction is added to the task processing instruction; the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
3. The inter-thread communication method according to claim 2, characterized in that: The obtaining of the task processing instruction for executing the main thread task includes: Obtaining the task processing mode of the main thread task; Obtaining a task processing function corresponding to the task processing mode as the task processing function corresponding to the main thread task; The task processing function corresponding to the main thread task is identified as a task processing instruction for executing the main thread task.
4. The inter-thread communication method according to claim 2, characterized in that: When executing the main thread task, detecting whether the sub-thread corresponding to the main thread has completed the sub-thread task by executing the task detection instruction in the task processing instruction of the main thread task includes: When the task processing instruction is called to execute the main thread task, if it is recognized that the task detection instruction is added to the task processing instruction, the task detection instruction is used to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task.
5. The inter-thread communication method according to claim 2, characterized in that: If the task corresponding to the processing result is completed, then after executing the main thread task, the method further includes: If all sub-thread tasks in the sub-thread task table are executed, the task detection instruction in the task processing instruction is deleted.
6. The inter-thread communication method according to claim 1, characterized in that: If the sub-thread has not completed the sub-thread task and the remaining completion time of the sub-thread task is less than the preset waiting time, monitoring the sub-thread task includes: If the sub-thread has not completed the sub-thread task, determining the remaining completion time of the sub-thread task; If the remaining completion time is less than the preset waiting time, the sub-thread task is monitored.
7. The inter-thread communication method according to claim 6, characterized in that: Determining the remaining completion time of the sub-thread task includes: Obtain the remaining tasks of the sub-thread task; The remaining tasks of the sub-thread task are input into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
8. An inter-thread communication device, characterized in that: Applied to the main thread, including: The task detection unit is configured to detect whether the sub-thread corresponding to the main thread has completed the sub-thread task by executing the task detection instruction in the task processing instruction of the main thread task when executing the main thread task; A result acquisition unit is configured to acquire a processing result of the sub-thread task if the sub-thread completes the sub-thread task; if the sub-thread does not complete the sub-thread task and the remaining completion time of the sub-thread task is less than a preset waiting time, monitor the sub-thread task, and if it is monitored that the sub-thread task is completed, acquire the processing result of the sub-thread task; A task creation unit is configured to create a task corresponding to the processing result and process the task corresponding to the processing result; the task corresponding to the processing result is used to represent a task related to the processing result and needs to be executed in the main thread; a task execution unit, configured to execute the main thread task if the task corresponding to the processing result is completed; the priority of the task corresponding to the processing result is higher than the priority of the main thread task; The task detection unit is further configured to execute acquisition of a semaphore corresponding to the sub-thread task; if the semaphore is a target semaphore, confirm that the sub-thread has completed the sub-thread task; the target semaphore is used to indicate that the sub-thread task has been completed.
9. The inter-thread communication device according to claim 8, characterized in that: The device also includes an instruction adding unit configured to execute a task processing instruction to obtain and execute the main thread task; add a task detection instruction to the task processing instruction; and the task detection instruction is used to detect whether the sub-thread has completed the sub-thread task.
10. The inter-thread communication device according to claim 9, characterized in that: The instruction adding unit is also configured to execute the task processing method for obtaining the main thread task; obtain the task processing function corresponding to the task processing method as the task processing function corresponding to the main thread task; and identify the task processing function corresponding to the main thread task as a task processing instruction for executing the main thread task.
11. The inter-thread communication device according to claim 9, characterized in that: The task detection unit is further configured to execute, when calling the task processing instruction to execute the main thread task, if it is recognized that the task detection instruction is added to the task processing instruction, then detect through the task detection instruction whether the sub-thread corresponding to the main thread has completed the sub-thread task.
12. The inter-thread communication device according to claim 9, characterized in that: The device also includes an instruction deleting unit configured to delete the task detection instruction in the task processing instruction if all sub-thread tasks in the sub-thread task table are executed.
13. The inter-thread communication device according to claim 8, characterized in that: The result acquisition unit is further configured to determine the remaining completion time of the sub-thread task if the sub-thread has not completed the sub-thread task; if the remaining completion time is less than the preset waiting time, monitor the sub-thread task.
14. The inter-thread communication device according to claim 13, characterized in that: The result acquisition unit is also configured to execute the acquisition of the remaining tasks of the sub-thread task; input the remaining tasks of the sub-thread task into a pre-trained time prediction model to obtain the completion time of the remaining tasks as the remaining completion time of the sub-thread task; the pre-trained time prediction model is used to predict the completion time of the input remaining tasks.
15. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the inter-thread communication method according to any one of claims 1 to 7.
16. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the inter-thread communication method according to any one of claims 1 to 7.
17. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the inter-thread communication method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Interface drawing method and device and mobile terminal
CN109669752A
Multi-thread network distribution method and storage medium
CN110580188A
Asynchronous compression processing method for large-volume JSON data
CN112328607A