CN114661481A - Control method and terminal among multithreading multi-mutex in single process - Google Patents

Control method and terminal among multithreading multi-mutex in single process Download PDF

Info

Publication number
CN114661481A
CN114661481A CN202210571806.0A CN202210571806A CN114661481A CN 114661481 A CN114661481 A CN 114661481A CN 202210571806 A CN202210571806 A CN 202210571806A CN 114661481 A CN114661481 A CN 114661481A
Authority
CN
China
Prior art keywords
thread
semaphore
threads
resource
mutual exclusion
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202210571806.0A
Other languages
Chinese (zh)
Other versions
CN114661481B (en
Inventor
宋久才
张常华
朱正辉
赵定金
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guangdong Baolun Electronics Co ltd
Original Assignee
Guangzhou Baolun Electronics Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guangzhou Baolun Electronics Co Ltd filed Critical Guangzhou Baolun Electronics Co Ltd
Priority to CN202210571806.0A priority Critical patent/CN114661481B/en
Publication of CN114661481A publication Critical patent/CN114661481A/en
Application granted granted Critical
Publication of CN114661481B publication Critical patent/CN114661481B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5018Thread allocation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5021Priority

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Cable Transmission Systems, Equalization Of Radio And Reduction Of Echo (AREA)
  • Multi Processors (AREA)

Abstract

The invention discloses a control method and a terminal among multithreading and multiple mutexes in a single process, wherein the method comprises the following steps: s1: obtaining the number N of threads, sequencing and recording the number of the threads as Tn(ii) a S2: initializing mutex semaphore, sequencing mutex semaphore, and recording mutex semaphore as AmFor the first (m-1) AmLocking is carried out; s3: thread TnAdding into the thread pool, and adding into the thread TnTransmitting a parameter i, and starting all threads; s4: judging whether i is equal to 1, if so, aligning the thread T1Introducing a mutual exclusion semaphore AmExecution of thread T1And for mutex semaphore A1Unlocking, if not, introducing a mutual exclusion semaphore A to the thread Tnm‑1Execution of thread TnAnd for mutex semaphore AnAnd (4) unlocking. In the invention, multithreading is driven to be orderly carried out by mutual exclusion semaphores,the principle of reasonable fairness can be embodied.

Description

Control method and terminal among multiple threads and multiple mutexes in single process
Technical Field
The invention relates to the technical field of information processing, in particular to a control method and a terminal among multiple threads and multiple mutexes in a single process.
Background
The modern operating system adopts a multi-channel programming mechanism, a plurality of processes can be executed concurrently, each process comprises a plurality of threads, and a CPU switches back and forth among the threads to share certain resources, so that the utilization rate of the resources is improved, but the conflict and mutual restriction relation among the plurality of threads which are executed concurrently are processed becomes a difficult problem. If the concurrent threads are not scheduled properly, the situation that the operation result is related to the switching time can occur, the result is not reproducible, the efficiency and the correctness of the system are influenced, and the system is directly crashed when the result is serious. Some mechanism is required to control this inter-constraint relationship between concurrent threads.
For example, chinese patent CN105511969A, a method for performing mutual exclusion between cross-process threads, includes the following steps: a plurality of threads compete for the mutual exclusion information, and the winning threads initialize the mutual exclusion information; after the winning thread obtains the mutual exclusion information, locking the critical area by using the mutual exclusion information; after locking operation is carried out, the thread obtains the lock of the mutual exclusion information and accesses the shared resource; meanwhile, other threads wait; after the thread uses up the shared resource, leaving the critical zone, and performing unlocking operation; the mutex information continues to be contended by other threads, and the above steps are repeated. However, the patent still suffers from the following problems: firstly, the execution sequence among a plurality of threads has no rule and is obviously in an uncontrollable unordered state; secondly, the method cannot be used for executing a plurality of threads with obvious causal relationships, so that the processing of the threads is time-consuming and has extremely low efficiency, and the problems of system crash, abnormal exit and the like are easy to occur.
Disclosure of Invention
In order to overcome the defects of the prior art, an object of the present invention is to provide a method for controlling multiple threads and multiple mutexes in a single process, which can solve the problem that the execution sequence among multiple threads is obviously in an uncontrollable unordered state without any rule.
The second purpose of the present invention is to provide a control terminal for multiple threads and multiple mutexes in a single process, which can solve the problem that the execution sequence among multiple threads is obviously in an uncontrollable unordered state without any rule.
In order to achieve one of the above purposes, the technical scheme adopted by the invention is as follows:
a control method among multithreading multi-mutexes in a single process comprises the following steps:
s1: acquiring the number N of threads in the process, sequencing the threads according to the priority, and recording the threads as TnWherein n is a serial number in the sequence;
s2: initializing N mutual exclusion semaphores, sequencing the mutual exclusion semaphores, and recording the mutual exclusion semaphores as AnFor the first (N-1) A in the sequencenLocking resources;
s3: will N threads TnAdding the threads into a thread pool one by one and adding the threads into each thread TnIn the process of transmitting the parameter i, all the threads T are startednWherein i = n;
s4: determining whether i is equal to 1, if yes, performing S5, and if no, performing S6;
s5: for thread T1Internally introducing a mutual exclusion semaphore A without resource lockingnExecution of the completion thread T1And for mutex semaphore A1Carrying out resource unlocking;
s6: for thread TnIs internally introduced with a mutual exclusion semaphore an-1Execution of the completion thread TnAnd for mutex semaphore AnAnd unlocking the resources.
Preferably, after executing the task of completing any one thread, the method further includes the following steps:
s7: acquiring preset thread exit conditions: ThreadExit = K, and the number of thread execution completion times Q, where Q has an initial value of 0;
s8: accumulating the thread execution completion times Q by +1 to obtain the thread execution current times B;
s9: judging whether B is equal to K, if so, ending the procedure, and if not, carrying out mutual exclusion semaphore AnAnd unlocking the resources.
Preferably, the step S5 is specifically implemented by the following steps:
s51: initialization temporary changeQuantity parameter AtempAnd on the thread T1Internally introducing a mutual exclusion semaphore A without resource lockingnValue assignment temporary variable parameter Atemp= mutex semaphore an
S52: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S52, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread T1The task of (2);
s53: for mutual exclusion semaphore A1The resource is unlocked, the next thread is triggered to execute, and the process returns to S52.
Preferably, the step S6 is specifically implemented by the following steps:
s61: initializing temporary variable parameter AtempAnd on the thread TnIs internally introduced with a mutual exclusion semaphore an-1Value assignment temporary variable parameter Atemp= mutex semaphore an-1
S62: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S62, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread TnThe task of (1);
s63: for mutual exclusion semaphore AnThe resource is unlocked, the next thread is triggered to execute, and the process returns to S62.
Preferably, the step S63 is specifically implemented by the following steps:
for mutual exclusion semaphore AnUnlocking the resource and triggering the execution thread Tn+1And returns to S62.
Preferably, the step S63 is specifically implemented by the following steps:
for mutual exclusion semaphore AnUnlocking the resource and triggering the execution thread T1And returns to S62.
In order to achieve the second purpose, the technical scheme adopted by the invention is as follows:
a control terminal among multithreading multi-mutexes in a single process comprises a storage and a processor;
a memory for storing program instructions;
a processor for executing the program instructions to execute the control method between multithreading and multiple mutexes in the single process
Compared with the prior art, the invention has the beneficial effects that: the method comprises the steps of obtaining the number N of threads in a process, sequencing the threads according to priority, simultaneously initializing N mutual exclusion semaphores, sequencing the mutual exclusion semaphores, and locking resources of the first (N-1) mutual exclusion semaphores in the sequencing; adding N threads into the thread pool one by one, and introducing the exclusive semaphore without resource locking into the thread T1By locking the mutex semaphore that is not resource locked, thread T is executed1To avoid the system repeatedly executing multiple threads T at the same time1The task of (1), forming a deadlock situation, and then for the mutual exclusion semaphore A1Unlocking the resource and setting the mutual exclusion semaphore A1Introduction into thread T2Then to the mutex semaphore A2Locking resources and executing thread T2To avoid the system repeatedly executing multiple threads T at the same time2The task forms the condition of deadlock, so on, drives a plurality of threads in the single process to execute according to the preset sequence, fully embodies the principle of reasonable fairness, and realizes the problem of efficiently and orderly processing multiple transactions.
Drawings
FIG. 1 is a flowchart illustrating a method for controlling multiple threads and multiple mutexes in a single process according to the present invention.
Detailed Description
The preferred embodiments of the present invention will be described in conjunction with the accompanying drawings, and it will be understood that they are described herein for the purpose of illustration and explanation and not limitation.
In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicate orientations or positional relationships based on the orientations or positional relationships shown in the drawings, and are only for convenience of description and simplicity of description, but do not indicate or imply that the device or element being referred to must have a particular orientation, be constructed and operated in a particular orientation, and thus, should not be construed as limiting the present invention. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance.
In the description of the present invention, it should be noted that, unless otherwise explicitly specified or limited, the terms "mounted," "connected," and "connected" are to be construed broadly, e.g., as meaning either a fixed connection, a removable connection, or an integral connection; can be mechanically or electrically connected; they may be connected directly or indirectly through intervening media, or they may be interconnected between two elements. The specific meanings of the above terms in the present invention can be understood in specific cases to those skilled in the art.
The invention will be further described with reference to the accompanying drawings and the detailed description below:
in the present invention, an auxiliary class, which is a thread synchronization class (denoted as a in the present application), is introduced, and the number of threads currently accessing itself, which is generally called Semaphore, may be used to control the number of threads accessing a specific resource at the same time, and by coordinating each thread, a reasonable resource usage is ensured, specifically, the Semaphore is configured as Semaphore (int initial count, int maximum count) and may be considered as a container for resource Semaphore, and the initial count and maximum count represent the initial resource count and maximum resource count respectively. The maximum number of resources maximumCount refers to the number of all resources contained in the Semaphore container. This maximum number of resources maximumCount includes currently available resources and resources that have not yet been released. The initial resource count initialCount refers to how many resources are released by Semaphore in the initial state of the program and can be directly used by the thread. Resource not yet released = maximumCount-initialCount. In the present invention, the initialization configuration is a (1,1), i.e., the initial resource and the maximum number of resources are both set to 1. Two methods are commonly used for Semaphore: WaitOne () and Release (): (1) the WaitOne () method can take out a resource from the existing available resources in the Semaphore resource container (the resource is taken out from the initial resource, not from the maximum number of resources), the initial resource is 1, after one resource is taken out, no resource is available, the current thread is prevented from executing, the thread waits, and the locking is performed; (2) release () can Release 1 resource from Semaphore, which can not only Release its own resources but also the resources that Semaphore was originally unavailable. Such as: the total resource of the resource container Semaphore (1,1) is 1, the initial available resource is 1, one initial resource can be released by using a Release () method, and the remaining (1-1) unavailable resources can be released, so that the Semaphore has 1 available resource; this is the lock reduction. The Semaphore, Waitone and Release form a complete system structure of mutual exclusion Semaphore.
Example one
As shown in fig. 1, a method for controlling multiple threads and multiple mutexes in a single process includes the following steps:
s1: acquiring the number N of threads in the process, sequencing the threads according to the priority, and recording the threads as TnWherein n is the sequence number of the thread in the sequence;
specifically, the number N of all threads in a single process is obtained, the threads are sorted according to the preset priority, and the threads are recorded as TnWhere n is the sequence number in the sequence, i.e. the thread is marked as: t is1、T2、T3、T4…… Tn
S2: initializing N mutual exclusion semaphores, sequencing the mutual exclusion semaphores, and recording the mutual exclusion semaphores as AnFor the first (N-1) A in the sequencenLocking resources;
specifically, N mutexes are initialized, each mutex is set (initial resource number =1, maximum available resource number = 1), and the mutexes are sorted while being recorded as anI.e. the mutex semaphore is denoted as A1、A2、A3…… AnThe first (N-1) A in the pair orderingnLocking resources, which is equivalent to locking A1、A2、A3…… An-1Resource locking is performed, now with and only AnNo locking action is performed, and preferably, the number of the mutex semaphores is not less than the number of the threads.
S3: will N threads TnAdding the threads into a thread pool one by one and adding the threads into each thread TnIn the middle, the parameter i is transmitted, and all the threads T are startednWherein i = n;
specifically, all T's are combinednAnd the parameters i are transmitted to each thread entering the thread pool, and then all the threads are started.
S4: judging whether i is equal to 1, if so, executing S5, otherwise, executing S6;
specifically, a parameter i of each thread is obtained, and it is determined whether i is equal to 1, if so, S5 is executed, and if not, S6 is executed.
S5: for thread T1Internally introducing a mutual exclusion semaphore A without resource lockingnExecution of the completion thread T1And for mutex semaphore A1Carrying out resource unlocking;
specifically, the mutex semaphore A without resource locking is usednIntroduction into thread T1At this time, other mutex semaphores are in resource locking state, so that the thread T is enabled1In this embodiment, the step S5 is implemented by the following steps:
s51: initializing temporary variable parameter AtempAnd on the thread T1Internally introducing a mutual exclusion semaphore A without resource lockingnValue assignment temporary variable parameter Atemp= mutex semaphore an
In particular, a temporary variable parameter A is addedtempAnd for temporary variable parameter AtempInitializing and simultaneously carrying out exclusive semaphore A of the unlocked resourcenIntroduction into thread T1In (2)Section for reassigning temporary variable parameter Atemp= mutex semaphore anLet a temporary variable parameter AtempThere is one resource available.
S52: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S52, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread T1The task of (1);
specifically, a temporary variable parameter A is judgedtempWhether the resource is in a locked state or not is judged, namely a temporary variable parameter A is judgedtempIf there is available resource, if temporary variable parameter AtempIn the resource locked state, the thread' S task continues to wait for execution and returns to S52 if the temporary variable parameter AtempIf not in the resource locking state, the temporary variable parameter A is settempLocking of resources (i.e. A)nAlso locked by resources), concurrently executes and completes thread T1To avoid execution of thread T1Task of (2), due to AnIs not locked by the resource, and thread T is repeated1I.e. multiple threads T at the same time1The task of (2) is executing, causing a deadlock.
Causes the system to execute and complete thread T1The task of (2).
S53: for mutual exclusion semaphore A1The resource is unlocked, the next thread is triggered to execute, and the process returns to S52.
In particular, thread T1After the task of (2) is executed, the mutual exclusion semaphore A is added1Unlocking the resource and triggering the execution of the next thread, e.g. thread T2And returns to S52 until AnAfter being unlocked, thread T is executed again1The task of (2).
S6: for thread TnIs internally introduced with a mutual exclusion semaphore an-1Execution of the completion thread TnAnd for mutex semaphore AnAnd unlocking the resources.
Specifically, n is a positive integer, and n in S6 should be greater than 1, so thread T is now presentnCan be used forIs T2、T3、T4……TnWhile mutually exclusive semaphores An-1Can be A1、A2、A3……An-1In this embodiment, the step S6 is specifically implemented by the following steps:
s61: initializing temporary variable parameter AtempAnd introducing a mutual exclusion semaphore A into the thread Tnn-1Value assignment temporary variable parameter Atemp=Mutual exclusion semaphore An-1
Specifically, a temporary variable parameter A is addedtempAnd for temporary variable parameter AtempInitializing and simultaneously carrying out mutual exclusion semaphore An-1Introduction into thread TnIs re-assigned with the temporary variable parameter atemp= mutex semaphore an-1(ii) a In this embodiment, the mutex semaphore A for which no resource locking is performednAdd to thread T1In (1), make thread T1Priority execution when thread T1After the execution is finished, the mutual exclusion semaphore A is added1Unlocking the circuit, and then setting the mutual exclusion semaphore A1Introduction into thread T2And so on, the unlocking sequence of the mutual exclusion semaphore is as follows: a. then- A1- A2-…- An-1The execution order of the threads is: t is1-T2-T3-…- TnAnd realizing the execution sequence of the defined threads.
S62: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S62, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread TnThe task of (1);
specifically, a temporary variable parameter A is judgedtempWhether the resource is in a locked state or not is judged, namely a temporary variable parameter A is judgedtempIf there is available resource, if temporary variable parameter AtempIn the resource locked state, the thread' S task continues to wait for execution and returns to S62 if the temporary variable parameter AtempIf the resource is not in the locking state, the temporary variable parameter A is settempTo lock resources (Namely An-1Also locked by resources), concurrently executes and completes thread TnTo avoid execution of thread TnTask of (2), due to An-1Is not locked by the resource, and thread T is repeatednI.e. multiple threads T at the same timenThe tasks of (2) are executed, resulting in deadlock, so that the execution sequence of the threads can only be: t is1-T2-T3-…- TnIf the previous thread is not executed, the next thread cannot be executed.
S63: for mutual exclusion semaphore AnThe resource is unlocked, the next thread is triggered to execute, and the process returns to S62.
In particular, thread TnAfter the task of (2) is executed, the mutual exclusion semaphore A is addednUnlocking the resource and triggering the execution of the next thread, e.g. thread T3And returns to S52 until An-1After being unlocked, thread T is executed againnIn this embodiment, the step S63 is specifically implemented by the following steps:
for mutual exclusion semaphore AnUnlocking the resource and triggering the execution thread Tn+1And returns to S62.
Preferably, when n is greater than 1, thread TnAfter the execution is finished, the mutual exclusion semaphore A is addednUnlocking the resource and triggering the execution thread Tn+1Specifically, thread T2After the execution is completed, the mutex semaphore A is checked2Unlocking the resource and then triggering the thread T3Execution, when thread T3After the execution is completed, the mutex semaphore A is checked3And unlocking the resource, and the like.
Preferably, the step S63 may be further implemented by:
for mutual exclusion semaphore AnUnlocking the resource and triggering the execution thread T1And returns to S62.
Preferably, when the Nth thread TnAfter the execution is completed, that is, the execution of all the N threads is completed once, the Nth mutual exclusion semaphore A is processednUnlocking the resource and triggering the execution thread T1Namely, resetting the execution sequence: t is1-T2-T3-…- TnA new cycle is started.
Preferably, after executing the task of completing any one thread, the method further includes the following steps:
s7: acquiring preset thread exit conditions: ThreadExit = K, and the number of times Q of thread execution is completed, wherein the initial value of Q is 0;
specifically, the thread exit condition is set in advance: ThreadExit = K, and acquires a thread exit condition each time a task of any one thread is completed: ThreadExit = K, and the number of thread execution completions Q
S8: accumulating the thread execution completion times Q by +1 operation to obtain the thread execution current times B;
specifically, after executing a task of completing any one thread, the execution completion number Q of the thread is accumulated to +1, that is, the current execution number B = Q +1 of the thread.
S9: judging whether B is equal to K, if so, ending the procedure, and if not, carrying out mutual exclusion semaphore AnAnd unlocking the resources.
Specifically, when B is equal to K, it indicates that the thread exit condition is currently met: ThreadExit = K, the procedure is ended, if B is less than K, the mutex semaphore A is setnAnd unlocking the resources, and further triggering and executing the next thread.
In this embodiment, if the number of threads in the process is 3 (T)1、T2、T3) Then initialize 3 mutex semaphores (A)1、A2、A3) To A, a1And A2Locking the resource, and locking T1、T2、T3Adding the threads into a thread pool one by one and adding the threads into each thread TnWith parameter i (i =1, 2, 3), all threads are started, i.e. T at this time1、T2、T3All in the starting state but not executing the task, and then A3Adding to T1In (A)1Adding to T2In (A) mixing2Is added to T3In the pair A3Locking is carried out, so that the system executes and completes T once1If B is equal to K, then B =0+1, if K is equal to 1, the procedure is ended, and if K is greater than 1, then a is executed1Unlock A, will A1Adding to T2In the pair A1Locking is carried out, so that the system executes and completes T once2If the value of K is equal to 2, the program is ended, and if K is greater than 2, the program is executed for a2Unlocking, A2Adding to T3In the pair A2Locking is carried out, so that the system executes and completes T once3If the value of K is equal to 3, the process is ended, and if K is greater than 3, the process is ended for a3Unlock and then pair A3Locking is carried out, so that the system executes and completes T once1Wherein the thread (T)1、T2、T3) The method is synchronous in execution and mutually exclusive, and avoids the blocking and deadlock of threads under the condition of ensuring high performance efficiency. Meanwhile, the work task sequence of the thread processing is executed strictly according to the set requirement, and the obtained result is also the result after the mutual cooperation processing among all threads.
Example two:
a control terminal among multithreading multi-mutexes in a single process comprises a storage and a processor;
a memory for storing program instructions;
the processor is configured to execute the program instructions to execute the method for controlling multiple threads and multiple mutexes in a single process according to the first embodiment.
Various other modifications and changes may occur to those skilled in the art based on the foregoing teachings and concepts, and all such modifications and changes are intended to be included within the scope of the appended claims.

Claims (7)

1. A control method among multithreading multi-mutexes in a single process is characterized by comprising the following steps:
s1: acquiring the number N of threads in the process, and aligning the threads according to the prioritySorting, and recording the thread as TnWherein n is a serial number in the sequence;
s2: initializing N mutual exclusion semaphores, sequencing the mutual exclusion semaphores, and recording the mutual exclusion semaphores as AnFor the first (N-1) A in the sequencenLocking resources;
s3: will N threads TnAdding the threads into a thread pool one by one and adding the threads to each thread TnIn the middle, the parameter i is transmitted, and all the threads T are startednWherein i = n;
s4: determining whether i is equal to 1, if yes, performing S5, and if no, performing S6;
s5: for thread T1Internally introducing a mutual exclusion semaphore A without resource lockingnExecution of the completion thread T1And for mutex semaphore A1Carrying out resource unlocking;
s6: for thread TnIs internally introduced with a mutual exclusion semaphore an-1Execution of the completion thread TnAnd for mutex semaphore AnAnd unlocking the resources.
2. The method for controlling multiple threads and multiple mutexes in a single process according to claim 1, wherein after the task of any one thread is completed, the method further comprises the steps of:
s7: acquiring preset thread exit conditions: ThreadExit = K, and the number of thread execution completion times Q, where Q has an initial value of 0;
s8: accumulating the thread execution completion times Q by +1 to obtain the thread execution current times B;
s9: judging whether B is equal to K, if so, ending the procedure, and if not, carrying out mutual exclusion semaphore AnAnd unlocking the resources.
3. The method for controlling multiple mutexes in a single process according to claim 2, wherein said S5 is implemented by:
s51: initializing temporary variable parameter AtempAnd on the thread T1Internal introduction ofMutex semaphore A without resource lockingnValue assignment temporary variable parameter Atemp= mutex semaphore an
S52: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S52, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread T1The task of (1);
s53: for mutual exclusion semaphore A1The resource is unlocked, the next thread is triggered to execute, and the process returns to S52.
4. The method for controlling multiple mutexes in a single process according to claim 2, wherein said S6 is implemented by:
s61: initializing temporary variable parameter AtempAnd on the thread TnIs internally introduced with a mutual exclusion semaphore an-1Value assignment temporary variable parameter Atemp= mutex semaphore an-1
S62: judging temporary variable parameter AtempWhether the resource is in a locked state; if so, the thread waits for execution and re-executes S62, otherwise, the temporary variable parameter A is settempLocking resources and executing a completion thread TnThe task of (1);
s63: for mutual exclusion semaphore AnThe resource is unlocked, the next thread is triggered to execute, and the process returns to S62.
5. The method for controlling multiple threads and multiple mutexes in a single process according to claim 4, wherein said step S63 is implemented by the steps of:
for mutual exclusion semaphore AnUnlocking the resource and triggering the execution thread Tn+1And returns to S62.
6. The method for controlling multiple threads and multiple mutexes in a single process according to claim 4, wherein said step S63 is implemented by the steps of:
for mutually exclusive semaphoreAnUnlocking the resource and triggering the execution thread T1And returns to S62.
7. A control terminal among multithreading multi-mutexes in a single process is characterized in that: comprises a storage and a processor;
a memory for storing program instructions;
a processor for executing the program instructions to perform the method of controlling among multiple threads and multiple mutexes in a single process according to any one of claims 1 to 6.
CN202210571806.0A 2022-05-25 2022-05-25 Control method and terminal among multithreading multi-mutex in single process Active CN114661481B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210571806.0A CN114661481B (en) 2022-05-25 2022-05-25 Control method and terminal among multithreading multi-mutex in single process

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210571806.0A CN114661481B (en) 2022-05-25 2022-05-25 Control method and terminal among multithreading multi-mutex in single process

Publications (2)

Publication Number Publication Date
CN114661481A true CN114661481A (en) 2022-06-24
CN114661481B CN114661481B (en) 2022-09-06

Family

ID=82038400

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210571806.0A Active CN114661481B (en) 2022-05-25 2022-05-25 Control method and terminal among multithreading multi-mutex in single process

Country Status (1)

Country Link
CN (1) CN114661481B (en)

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6237019B1 (en) * 1998-03-18 2001-05-22 International Business Machines Corporation Method and apparatus for performing a semaphore operation
US20030005195A1 (en) * 2001-06-27 2003-01-02 International Business Machines Corporation Semaphore management subsystem for use with multi-thread processor systems
CN1991767A (en) * 2005-12-27 2007-07-04 三菱电机株式会社 Semaphore management method and semaphore management program
CN105511969A (en) * 2015-11-25 2016-04-20 中国船舶工业***工程研究院 Inter-process thread mutex method
CN111913809A (en) * 2020-07-28 2020-11-10 北京百度网讯科技有限公司 Task execution method, device, equipment and storage medium under multi-thread scene
CN112486694A (en) * 2020-11-30 2021-03-12 山东浪潮通软信息科技有限公司 Network lock processing method and device based on Redis
CN114461409A (en) * 2022-01-13 2022-05-10 北京特立信电子技术股份有限公司 Auxiliary analysis method, system and storage medium for abnormal blocking of mutual exclusion semaphore

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6237019B1 (en) * 1998-03-18 2001-05-22 International Business Machines Corporation Method and apparatus for performing a semaphore operation
US20030005195A1 (en) * 2001-06-27 2003-01-02 International Business Machines Corporation Semaphore management subsystem for use with multi-thread processor systems
CN1991767A (en) * 2005-12-27 2007-07-04 三菱电机株式会社 Semaphore management method and semaphore management program
CN105511969A (en) * 2015-11-25 2016-04-20 中国船舶工业***工程研究院 Inter-process thread mutex method
CN111913809A (en) * 2020-07-28 2020-11-10 北京百度网讯科技有限公司 Task execution method, device, equipment and storage medium under multi-thread scene
CN112486694A (en) * 2020-11-30 2021-03-12 山东浪潮通软信息科技有限公司 Network lock processing method and device based on Redis
CN114461409A (en) * 2022-01-13 2022-05-10 北京特立信电子技术股份有限公司 Auxiliary analysis method, system and storage medium for abnormal blocking of mutual exclusion semaphore

Also Published As

Publication number Publication date
CN114661481B (en) 2022-09-06

Similar Documents

Publication Publication Date Title
US8966491B2 (en) System and method for implementing NUMA-aware reader-writer locks
US8930584B2 (en) System and method for providing a linearizable request manager
US8020166B2 (en) Dynamically controlling the number of busy waiters in a synchronization object
US20120192194A1 (en) Lock Free Acquisition and Release of a Semaphore in a Multi-Core Processor Environment
KR20090005921A (en) Load balancing method and apparatus in symmetric multi-processor system
EP3151116A1 (en) Vehicle control apparatus
CN106716348A (en) Shared resources in a data processing appartus for executing a plurality of threads
CN112506821B (en) System bus interface request arbitration method and related components
CN114661481B (en) Control method and terminal among multithreading multi-mutex in single process
CN112148480A (en) Task processing method, device and equipment based on multithreading and storage medium
CN111400015A (en) Task scheduling method and device
Sharma et al. Window-based greedy contention management for transactional memory
Nemitz et al. Multiprocessor real-time locking protocols for replicated resources
CN101189579A (en) Behavioral model based multi-threaded architecture
US20030131168A1 (en) Ensuring fairness in a multiprocessor environment using historical abuse recongnition in spinlock acquisition
CN110908968B (en) Method, device, equipment and storage medium for avoiding frightened groups during file lock unlocking
US11200098B2 (en) Reduction of interrupt service latency in multi-processor systems
Aggarwal et al. Lock-free and wait-free slot scheduling algorithms
CN115756856A (en) Spin lock hardware and method based on multi-core processor access
Aspnes et al. Lower bounds for restricted-use objects
Hasenfratz et al. Transactional memory: How to perform load adaption in a simple and distributed manner
CN116204303A (en) Implementation method of spin lock capable of optimizing interrupt delay
US11119831B2 (en) Systems and methods for interrupting latency optimized two-phase spinlock
JPH07319716A (en) Exclusive control system for resources of computer system
JPH0895807A (en) Task execution control method

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
CP03 Change of name, title or address

Address after: No. 56 Nanli East Road, Shiqi Town, Panyu District, Guangzhou City, Guangdong Province, 510000

Patentee after: Guangdong Baolun Electronics Co.,Ltd.

Address before: No.19 Chuangyuan Road, Zhongcun street, Panyu District, Guangzhou, Guangdong 510000

Patentee before: GUANGZHOU ITC ELECTRONIC TECHNOLOGY Co.,Ltd.

CP03 Change of name, title or address