A method, apparatus, and computer device for obtaining shared resources

By dividing the total resource pool into sub-resource pools and polling and locking to unlock, the competition problem of shared resources in multi-core and multi-threaded systems is solved, and efficient utilization of resources is achieved.

CN112114974BActive Publication Date: 2025-07-22HANGZHOU DPTECH INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202011103191.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-10-15
Publication Date
2025-07-22
Estimated Expiration
2040-10-15

AI Technical Summary

Technical Problem

In a multi-core and multi-threading system, the concurrent read and write of shared resources by multiple threads leads to program errors and waste of system resources. The existing technology leads to thread competition through locking mechanisms and causes waste of resources.

Method used

The total resource pool is divided into several sub-resource pools. After the thread determines that there are free resources in the total resource pool, it will poll the sub-resource pool and query whether there are free resources. After obtaining, it will unlock all locked sub-resource pools.

Benefits of technology

Multiple threads are implemented to obtain shared resources at the same time, avoiding waste of system resources and improving resource utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112114974B_ABST
    Figure CN112114974B_ABST
Patent Text Reader

Abstract

This specification provides a method, apparatus, and computer device for obtaining shared resources. The method includes: determining whether there are idle resources in the total resource pool, where the total resource pool is divided into several sub-resource pools; when there are idle resources in the total resource pool, polling each of the sub-resource pools, locking the polled sub-resource pool and then querying whether there are idle resources; when idle resources are obtained, unlocking all the locked sub-resource pools.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This specification relates to the field of computer technology, and particularly to a method and apparatus for obtaining shared resources and a computer device. Background Art

[0002] Currently, when processing a large amount of information, a multi-core and multi-threaded system is used to process the information to improve the information processing efficiency. However, since multiple threads can run simultaneously, there will be concurrent reading and writing of shared resources by multiple threads, resulting in processing errors. In related technologies, generally a lock is introduced to ensure that only one thread can occupy the shared resource at the same time. However, since multiple threads need to compete for the same lock, only the thread that holds the lock can access the shared resource, and other threads can only wait, which will cause a great waste of system resources. Summary of the Invention

[0003] To overcome the problems in the related technologies, this specification provides a method and apparatus for obtaining shared resources and a computer device.

[0004] According to a first aspect of the embodiments of this specification, a method for obtaining shared resources is provided, which is applied to a multi-core and multi-threaded processing system. The method includes:

[0005] Determine whether there is idle resource in the total resource pool, where the total resource pool is divided into several sub-resource pools;

[0006] When there is idle resource in the total resource pool, poll each of the sub-resource pools, lock the polled sub-resource pool, and then query whether there is idle resource;

[0007] When obtaining the idle resource, unlock all the locked sub-resource pools.

[0008] According to a second aspect of the embodiments of this specification, a device for obtaining shared resources is provided, which is applied to a multi-core and multi-threaded processing system. The device includes:

[0009] A determination unit, configured to determine whether there is idle resource in the total resource pool, where the total resource pool is divided into several sub-resource pools;

[0010] A polling unit, configured to poll each of the sub-resource pools when there is idle resource in the total resource pool, lock the polled sub-resource pool, and then query whether there is idle resource;

[0011] An unlocking unit, configured to unlock all the locked sub-resource pools when obtaining the idle resource.

[0012] According to a third aspect of the embodiments of the present specification, a computer device is provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the method of the first aspect of the above embodiments is implemented.

[0013] In the technical solution provided by the embodiments of the present specification, the total resource pool is divided into several sub-resource pools. After determining that there are idle resources in the total resource pool, the thread polls each sub-resource pool, locks the polled sub-resource pool first, and then checks whether there are idle resources. If no idle resources are obtained, the current sub-resource pool is not unlocked, and the next sub-resource pool is polled until idle resources are obtained and then all the locked sub-resource pools are unlocked. This application allows multiple threads to obtain shared resources simultaneously, enabling the effective use of the idle resources in the total resource pool and overcoming the problem of system resource waste in the prior art.

[0014] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present specification. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, showing embodiments consistent with the present specification, and are used together with the specification to explain the principles of the present specification.

[0016] Figure 1 is a schematic diagram of a method for obtaining shared resources in a multi-core and multi-threaded system;

[0017] Figure 2 is a schematic diagram of another method for obtaining shared resources in a multi-core and multi-threaded system;

[0018] Figure 3 is a flowchart of a method for obtaining shared resources according to an exemplary embodiment of the present specification;

[0019] Figure 4 is a schematic diagram of a method for two threads to obtain shared resources simultaneously according to an exemplary embodiment of the present specification;

[0020] Figure 5 is a structural diagram of a device for obtaining shared resources according to an exemplary embodiment of the present specification;

[0021] Figure 6 is a schematic diagram of a computer device for obtaining shared resources according to an exemplary embodiment of the present specification. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0022] Exemplary embodiments will be described in detail herein, and examples thereof are shown in the accompanying drawings. When the following description refers to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this specification. On the contrary, they are merely examples of devices and methods consistent with some aspects of this specification as detailed in the appended claims.

[0023] The terms used in this specification are for the purpose of describing particular embodiments only and are not intended to limit this specification. The singular forms "a", "the", and "said" used in this specification and the appended claims are also intended to include the plural forms unless the context clearly dictates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.

[0024] It should be understood that although the terms first, second, third, etc. may be used in this specification to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of this specification, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to determining".

[0025] When processing a large amount of information, the efficiency of processing information by a single-core multi-threaded processor is limited. To improve the processing efficiency, a multi-core processor (Multiple Cores Processor, MCP) is used to process information. A multi-core processor refers to a processor that integrates two or more complete cores (a core is also called a computing engine) in one processor. Each core of a multi-core processor can independently execute one or more threads, so a multi-core processor can achieve true parallel processing.

[0026] When using a multi-core processor to process information, due to the existence of multiple threads, concurrent read and write operations on shared resources are often involved. Taking Figure 1 as an example, Figure 1It is a quad-core and four-thread design where the nuclear CPUs 1, 2, 3, and 4 run threads 1, 2, 3, and 4 respectively, and shared resources are stored in the resource pool. Since multiple threads in this system can run simultaneously, when multiple threads read or write to the shared resources in the total resource pool at the same time, it will cause program errors. For example, thread 1 is currently using the shared resource a in the resource pool and writes the shared resource a as "1". At this time, thread 1 can run normally. However, if thread 2 then accesses the shared resource a and writes the shared resource a as "0" so that it can run normally. And thread 1 can run normally only when the shared resource a is "1", and the write operation of thread 2 to the shared resource a will cause thread 1 to error.

[0027] To ensure the correctness of multi-threaded programs and solve the contention problem of shared resources, locks are generally introduced between threads and shared resources. See Figure 2 , when multiple threads of a multi-core and multi-thread system start running, the CPUs corresponding to the threads will apply for locks for the threads. If the locks can be successfully acquired, the threads can obtain the idle resources in the shared resources and perform read and write operations on the idle resources; if the locks cannot be successfully acquired, the threads can only wait. Since in this method, multiple threads need to compete for the same lock, when a thread holds the lock, if other threads need to access the shared resources, they can only choose to wait, which will cause a great waste of system resources.

[0028] To overcome the above problems and improve the utilization rate of system resources, this specification provides a method, device, and computer device for obtaining shared resources.

[0029] Next, the embodiments of this specification will be described in detail.

[0030] As Figure 3 shown, Figure 3 is a flowchart of a method for obtaining shared resources shown according to an exemplary embodiment of this specification. The method provided by the embodiments of this application can be executed by any terminal device and / or server with computing and processing capabilities, and this application does not make any limitations in this regard. As Figure 3 shown, the method provided by the embodiments of this application can include the following steps:

[0031] In step 302, it is judged whether there are idle resources in the total resource pool, where the total resource pool is divided into several sub-resource pools;

[0032] In step 304, when there are idle resources in the total resource pool, poll each of the sub-resource pools, and after locking the polled sub-resource pool, query whether there are idle resources again;

[0033] In step 306, when free resources are acquired, all locked sub-resource pools are unlocked.

[0034] Next, combine Figure 4 , taking the example of thread 1 of CPU1 applying for free resources from the total resource pool, the shared resource acquisition method of the present application is explained. Figure 4 The total resource pool in stores shared resources, and according to the number of threads in the multi-core multi-threaded system, the total resource pool is divided into the same number of sub-resource pools. Figure 4 In the multi-core multi-threaded system, there are 6 threads, and the total resource pool is divided into 6 sub-resource pools. When thread 1 of CPU 1 needs to apply for resources from the total resource pool, it first determines whether there are idle resources in the total resource pool.

[0035] In some embodiments, whether there are idle resources in the total resource pool can be determined based on the value of the counter of the total resource pool. The counter can be a system-provided counter that counts the idle resources in the total resource pool, or it can be an additional counter that counts the idle resources in the total resource pool, and this application does not limit it. In addition, those skilled in the art should understand that other methods can also be used, such as querying the resource utilization rate of the total resource pool, to determine whether there are idle resources in the total resource pool.

[0036] When judging whether there are free resources in the total resource pool based on the value of the counter of the total resource pool, optionally, if the value of the counter of the total resource pool is greater than 0, it indicates that there are free resources in the total resource pool; if the value of the counter of the total resource pool is not greater than 0, it indicates that there are no free resources in the total resource pool.

[0037] In some embodiments, when it is determined that there are idle resources in the total resource pool, the value of the counter of the total resource pool is changed. Figure 4Taking the case where Thread 1 in [the system] applies for idle resources from the total resource pool as an example for illustration. Thread 1 wants to apply for idle resources from the total resource pool. After determining that the value of the counter in the total resource pool is greater than 0, it confirms that there are idle resources in the current total resource pool and can request idle resources from the total resource pool. However, since it doesn't know the specific location of the available idle resources, Thread 1 needs to use the polling method to obtain the available idle resources. If it just determines that there are available resources in the total resource pool and then queries for idle resources, then, if the number of available resources in the total resource pool is less than the number of threads applying for resources, there will be a problem that the thread will only find that there are no idle resources after polling all the sub-resource pools. Therefore, after each thread applying for resources determines that there are idle resources in the total resource pool, the count value of the counter in the total resource pool should also be decreased by 1. This operation occurs before the actual acquisition of the idle resources and is actually a kind of pre-occupation of resources. The purpose is to notify the subsequent threads applying for resources whether there are still available idle resources in the total resource pool. If the counter is an additional counter, then directly notify the counter to decrease by 1. If the counter is a system-built-in counter, those skilled in the art should understand that the counter also needs to be adaptively modified so that for the same thread applying for resources, when the thread pre-occupies and actually occupies the idle resources, the value of the counter only decreases by 1.

[0038] After the thread confirms that there are idle resources in the total resource pool, it polls each sub-resource pool, locks the polled sub-resource pool, and then queries whether there are idle resources.

[0039] In some embodiments, when polling each sub-resource pool, a sub-resource pool can be selected as the polling starting point and polled in a preset order.

[0040] In some embodiments, according to the number of threads in the multi-core and multi-thread system, the total resource pool is divided into the same number of sub-resource pools, and each sub-resource pool is respectively bound to a thread. When the specified thread polls each sub-resource pool, the sub-resource pool bound to the thread is selected as the polling starting point. Still taking Figure 4 as an example for illustration, the 6 threads in the multi-core and multi-thread system are respectively bound to the 6 sub-resource pools of the total resource pool, that is: Thread 1 is bound to Sub-resource Pool 1, Thread 2 is bound to Sub-resource Pool 2... When the thread polls the sub-resource pools, it starts polling from the sub-resource pool bound to it. For example, when Thread 3 polls each sub-resource pool to find idle resources, it starts polling from Sub-resource Pool 3 bound to Thread 3.

[0041] Of course, the thread can also select a sub-resource pool as the starting sub-resource pool for polling in other ways so that the thread can obtain idle resources more efficiently. The above-mentioned method of selecting the starting sub-resource pool in this application is only an example.

[0042] In some embodiments, while the total resources are divided into multiple sub - resource pools, they are also pre - numbered. When a thread queries for idle resources, it polls in the order of the numbers of the sub - resource pools. Still in combination with Figure 4 it is described as follows. Thread 1 wants to obtain idle resources. After determining that there are idle resources in the total resource pool, it starts from sub - resource pool 1 and polls sub - resource pool 2, sub - resource pool 3,... in the order of the numbers of the sub - resource pools. In another case, when thread 3 wants to obtain idle resources, after determining that there are idle resources in the total resource pool before obtaining idle resources, it starts from sub - resource pool 3 and polls sub - resource pool 4, sub - resource pool 5, sub - resource pool 6 in turn. Since sub - resource pool 6 is the last sub - resource pool, after polling sub - resource pool 6, thread 3 starts from sub - resource pool 1 and polls sub - resource pool 1, sub - resource pool 2 in turn. Of course, the thread can also poll the sub - resource pools in other preset polling orders to obtain idle resources, and this application does not make any restrictions.

[0043] In step 304 of this application, when the thread determines that there are idle resources in the total resource pool, it locks the polled sub - resource pool and then queries whether there are idle resources.

[0044] In some embodiments, determining whether there are idle resources in the currently locked sub - resource pool includes: determining whether there are idle resources in the current sub - resource pool according to the value of the second counter; the second counter is used to count the idle resources in the current sub - resource pool.

[0045] Here, taking Figure 4Taking the case where thread 3 in [the context] obtains idle resources as an example, it will be described in combination with the foregoing embodiments. When thread 3 needs to obtain idle resources, it first determines whether the total resource count in the total resource pool is greater than 0. If it is greater than 0, it means that there are available idle resources in the total resource pool, so the total resource count in the total resource pool is decremented by 1, and the idle resource is pre-occupied. Then, thread 3 starts polling from sub-resource pool 3: first, lock sub-resource pool 3, and then, according to the value of the counter in sub-resource pool 3, determine whether there are idle resources in sub-resource pool 3. If there are idle resources in sub-resource pool 3, obtain the idle resources and unlock sub-resource pool 3; if there are no idle resources in sub-resource pool 3, thread 3 does not unlock sub-resource pool 3, but continues to lock sub-resource pool 4. Then, according to the count value in sub-resource pool 4, determine whether there are idle resources in sub-resource pool 4. If there are idle resources in sub-resource pool 4, obtain the idle resources and unlock sub-resource pool 3 and sub-resource pool 4; if there are no idle resources in sub-resource pool 4, the thread continues to lock sub-resource pool 5, and so on. That is to say, when polling each sub-resource pool, first lock the current sub-resource pool, and then, according to the value of the counter in the current sub-resource pool, determine whether there are idle resources in the current sub-resource pool. If not, do not unlock the current sub-resource pool, continue to lock the next sub-resource pool and determine whether there are idle resources, until idle resources are obtained, then unlock all the previously locked sub-resource pools together.

[0046] In some embodiments, the lock added to the sub-resource pool can be a mutex lock or a spin lock. The "lock" in a multi-core and multi-thread system, embodied in code form, can ensure that only one thread accesses the shared resources at the same time. A mutex lock is such that when the task of the previous thread has not been completed (i.e., the shared resources are locked), then the next thread will enter the sleep state and wait for the task to be completed. When the task of the previous thread is completed, the next thread will be automatically awakened and then execute the task. The spin lock, different from the mutex lock, is that when the task of the previous thread has not been completed (i.e., the shared resources are locked), then the next thread will keep waiting (without sleeping), and when the task of the previous thread is completed, the next thread will execute immediately. In this application, the above two forms of locks are only for illustrative purposes, and those skilled in the art should understand that other forms of locks can also be used to ensure that only one thread accesses the shared resources in the sub-resource pool at the same time.

[0047] In some embodiments, when a thread obtains an idle resource in a sub - resource pool, the value of the counter used to count the idle resources in the current sub - resource pool is changed. Optionally, when a thread obtains an idle resource in a sub - resource pool, the value of the counter of the sub - resource pool is decremented by 1, so that the value of the counter can reflect the resource status of the current sub - resource pool in real time. Therefore, only when the value of the counter of the sub - resource pool is greater than 0 will the thread obtain an idle resource; when the value of the counter of the sub - resource pool is not greater than 0, the thread can immediately determine that there is no idle resource in the current sub - resource pool.

[0048] In some embodiments, when an idle resource is obtained, the resource pool number of the obtained idle resource is marked as the number of the current sub - resource pool. Therefore, when a thread releases the occupied resource, the resource can be released back to its original sub - resource pool according to the resource pool number in the resource.

[0049] In some embodiments, while releasing the resource back to its original sub - resource pool, the values of the resource counters of the total resource pool and the corresponding sub - resource pool are also changed simultaneously. For example, the resource counters of the total resource pool and the corresponding sub - resource pool are both incremented by 1, so that the values of the resource counters can reflect the resource usage status of the total resource pool and the sub - resource pool in real time.

[0050] Next, in combination with the above - mentioned various embodiments, taking two threads obtaining idle resources as an example, this application illustrates that this application can allow multiple threads to obtain shared resources simultaneously, and can effectively use the idle resources in the total resource pool.

[0051] See Figure 4 , assuming that there are 2 idle resources in the current total resource pool, thread 1 of CPU1 and thread 3 of CPU3 need to obtain idle resources, and thread 1 applies for idle resources slightly earlier than thread 2. For thread 1 to obtain an idle resource, it first determines whether the count of the total resource pool is greater than 0. It is found that the count of the total resource pool is 2, so the count of the total resource pool is decremented by 1, and one resource is pre - occupied. Then it starts polling from the sub - resource pool 1 bound to it. First, it locks sub - resource pool 1, and then determines whether the count value of sub - resource pool 1 is greater than 0. If it is greater than 0, it means there is an idle resource in sub - resource pool 1, and it directly obtains the idle resource and unlocks sub - resource pool 1; if it is not greater than 0, it means there is no idle resource in sub - resource pool 1, and it continues to lock sub - resource pool 2 and determines whether there is an idle resource in sub - resource pool 2. If the count value of sub - resource pool 2 is greater than 0, it means there is an idle resource in sub - resource pool 2, then thread 1 obtains the idle resource and unlocks sub - resource pool 1 and sub - resource pool 2; if the count value of sub - resource pool 2 is not greater than 0, it means there is no idle resource in sub - resource pool 2, and it continues to lock sub - resource pool 3 and determines whether there is an idle resource in sub - resource pool 3, and so on, until it obtains an idle resource and then unlocks all the sub - resource pools locked by thread 1.

[0052] If, before Thread 1 locks Sub-resource Pool 3, Thread 3 also needs to obtain an idle resource, Thread 3 first checks whether the total resource count in the total resource pool is greater than 0. Since the total resource count in the total resource pool is 1 at this time, which is greater than 0, Thread 3 determines that there is an idle resource in the total resource pool, so it decrements the total resource count by 1 and pre-occupies an idle resource. Then, Thread 3 starts polling from Sub-resource Pool 3. It first locks the sub-resource pool, and then checks whether there is an idle resource in the current sub-resource pool. If there is no idle resource, it locks the next sub-resource pool and checks whether there is an idle resource; if there is, it obtains the idle resource and unlocks the sub-resource pool that was locked by this thread before.

[0053] When Thread 1 polls Sub-resource Pool 3 and finds that Sub-resource Pool 3 is locked, Thread 1 can choose to wait until the resource pool 3 is unlocked (i.e., the lock is a "spin lock"), and continue polling from Sub-resource Pool 3; or it can choose to abandon the application for this idle resource and re-apply for an idle resource from Sub-resource Pool 1 or Sub-resource Pool 3 after a certain period of time (i.e., the lock is a "mutex lock").

[0054] If, after Thread 1 locks Sub-resource Pool 3, Thread 3 also needs to obtain an idle resource, Thread 3 first checks whether the total resource count in the total resource pool is greater than 0. Since the total resource count in the total resource pool is 1 at this time, which is greater than 0, Thread 3 determines that there is an idle resource in the total resource pool, so it decrements the total resource count by 1 and pre-occupies an idle resource. Then, Thread 3 starts polling from Sub-resource Pool 3. However, Thread 3 finds that Sub-resource Pool 3 is locked, so Thread 3 can choose to wait until the resource pool 3 is unlocked (i.e., the lock is a "spin lock"), and continue polling from Sub-resource Pool 3; or it can choose to abandon the application for this idle resource and re-apply for an idle resource from Sub-resource Pool 3 after a certain period of time (i.e., the lock is a "mutex lock").

[0055] In some embodiments, it may occur that the resources originally occupied by a thread return to the total resource pool again. However, due to the method of locking all the polled sub-resource pools in this application before the thread obtains an idle resource, therefore, the newly released idle resources can only appear in the sub-resource pools that have not been polled. Therefore, it does not affect the thread's acquisition of idle resources in the sub-resource pools.

[0056] From the above embodiments, it can be seen that since the thread first checks that there must be an idle resource in the total resource pool before applying for a resource, therefore, through the polling method, the thread can definitely obtain an idle resource. When there are multiple idle resources in the total resource pool, the method of this application allows multiple threads to apply for idle resources simultaneously, which can make full use of the resources in the total resource pool and avoid waste of system resources.

[0057] Corresponding to the embodiments of the foregoing method for obtaining shared resources, the present application also provides embodiments of a device for obtaining shared resources.

[0058] As Figure 5 shown, Figure 5 FIG. is a structural diagram of a device for obtaining shared resources according to an exemplary embodiment of the present application. The device includes: a determination unit 510, a polling unit 520, and an unlocking unit 530.

[0059] The determination unit 510 is configured to determine whether there are idle resources in the total resource pool, where the total resource pool is divided into a plurality of sub-resource pools;

[0060] The polling unit 520 is configured to, when there are idle resources in the total resource pool, poll each of the sub-resource pools, lock the polled sub-resource pool, and then query whether there are idle resources;

[0061] The unlocking unit 530 is configured to unlock all the locked sub-resource pools when idle resources are obtained.

[0062] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can refer to the partial descriptions of the method embodiments. The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated. The components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the present disclosure solution. Those of ordinary skill in the art can understand and implement it without creative efforts.

[0063] As can be seen from the above embodiments, since the thread first determines whether there are definitely idle resources in the total resource pool before applying for resources, therefore, by means of polling, the thread can definitely obtain idle resources. When there are multiple idle resources in the total resource pool, the method of the present application allows multiple threads to apply for idle resources simultaneously, which can make the resources in the total resource pool be fully utilized and avoid waste of system resources.

[0064] See Figure 6, which is a schematic structural diagram of a computer device provided by an embodiment of the present application. Specifically, the computer device at least includes a memory 610, a processor 620, and a computer program stored on the memory and executable on the processor. The memory 610 may include a volatile memory; the memory 610 may also include a non-volatile memory; the memory 610 may further include a combination of the above types of memories. The processor 620 may be a central processing unit (CPU). The processor 620 may further include a hardware video image processing device. The above hardware video image processing device may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. Specifically, for example, it may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), or any combination thereof. When the stored computer program is executed by the processor, all embodiments of the above method of the present application are implemented, which will not be elaborated here.

[0065] Those of ordinary skill in the art can understand that all or part of the processes in the above method embodiments can be completed by instructing relevant hardware through a computer program. The program can be stored in a computer-readable storage medium. When the program is executed, it may include the processes of the above method embodiments. Among them, the storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM), etc.

[0066] The above describes specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be executed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the drawings do not necessarily require the specific order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0067] Those skilled in the art will readily conceive of other embodiments of the present specification after considering the specification and practicing the invention claimed herein. The present specification is intended to cover any variations, uses, or adaptations of the present specification, which follow the general principles of the present specification and include common general knowledge or conventional technical means in the technical field not claimed in the present specification. The specification and examples are only regarded as exemplary, and the true scope and spirit of the present specification are pointed out by the claims.

[0068] It should be understood that the present specification is not limited to the exact structures described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the present specification is only limited by the appended claims.

[0069] The above are only the preferred embodiments of the present specification and are not intended to limit the present specification. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present specification shall be included within the scope of protection of the present specification.

Claims

1. A method for obtaining shared resources, which is applied to a multi-core and multi-thread processing system, characterized in that The method includes: Determine whether there is idle resource in the total resource pool, where the total resource pool is divided into several sub-resource pools; When there is idle resource in the total resource pool, pre-occupy one idle resource, and poll each of the sub-resource pools, and after locking the polled sub-resource pool, query whether there is idle resource; When idle resource is obtained, unlock all the locked sub-resource pools.

2. The method according to claim 1, wherein The determination of whether there is idle resource in the total resource pool includes: Determine whether there is idle resource in the total resource pool according to the value of the first counter; The first counter is used to count the idle resources in the total resource pool.

3. The method according to claim 2, wherein The method further includes: when it is determined that there is idle resource in the total resource pool, change the value of the first counter.

4. The method according to claim 1, characterized in that, The method is executed by a specified thread, and the first sub-resource pool to be polled is the sub-resource pool pre-bound to the thread.

5. The method according to claim 1, wherein Each sub-resource pool in the total resource pool is pre-numbered to poll each of the sub-resource pools based on the number.

6. The method according to claim 1, characterized in that, The determination of whether there is idle resource in the currently locked sub-resource pool includes: Determine whether there is idle resource in the current sub-resource pool according to the value of the second counter; The second counter is used to count the idle resources in the current sub-resource pool.

7. The method according to claim 1, wherein Lock the sub-resource pool, and the lock is a mutex lock or a spin lock.

8. The method according to claim 6, wherein It further includes: When idle resource is obtained, change the value of the second counter.

9. A shared resource acquisition device, applied to a multi-core and multi-thread processing system, is characterized in that The device includes: A determination unit, used to determine whether there is idle resource in the total resource pool, where the total resource pool is divided into several sub-resource pools; A polling unit, used to pre-occupy one idle resource when there is idle resource in the total resource pool, and poll each of the sub-resource pools, and after locking the polled sub-resource pool, query whether there is idle resource; An unlocking unit, used to unlock all the locked sub-resource pools when idle resource is obtained.

10. A computer device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein, When the processor executes the program, it implements the method according to any one of claims 1-8.

Citation Information

Patent Citations

  • Group shared resource sharing method and system

    CN111367660A