CN106980544A - A kind of thread synchronization method and thread synchronization system - Google Patents
A kind of thread synchronization method and thread synchronization system Download PDFInfo
- Publication number
- CN106980544A CN106980544A CN201710208287.0A CN201710208287A CN106980544A CN 106980544 A CN106980544 A CN 106980544A CN 201710208287 A CN201710208287 A CN 201710208287A CN 106980544 A CN106980544 A CN 106980544A
- Authority
- CN
- China
- Prior art keywords
- variable
- state
- thread
- protection zone
- status
- 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
Links
- 238000000034 method Methods 0.000 title claims abstract description 49
- 230000007717 exclusion Effects 0.000 claims description 32
- 238000010276 construction Methods 0.000 claims description 2
- 238000007599 discharging Methods 0.000 abstract description 2
- 230000008569 process Effects 0.000 description 10
- 238000005516 engineering process Methods 0.000 description 3
- 241001269238 Data Species 0.000 description 2
- 230000008878 coupling Effects 0.000 description 2
- 238000010168 coupling process Methods 0.000 description 2
- 238000005859 coupling reaction Methods 0.000 description 2
- 230000007423 decrease Effects 0.000 description 2
- 230000008859 change Effects 0.000 description 1
- 238000004590 computer program Methods 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000000750 progressive effect Effects 0.000 description 1
- 230000004044 response Effects 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/46—Multiprogramming arrangements
- G06F9/52—Program synchronisation; Mutual exclusion, e.g. by means of semaphores
- G06F9/526—Mutual exclusion algorithms
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Debugging And Monitoring (AREA)
Abstract
This application discloses a kind of thread synchronization method and thread synchronization system; wherein; the state of first variable is changed to first state by the thread synchronization method after ensureing any protection zone success of thread accesses by the destructor function; to discharge the locking during the thread accesses for the protection zone, so as to realize the purpose for discharging the protection zone in time;And when the thread for accessing the protection zone is killed, the normal operation of destructor function is not interfered with yet, is killed and possible caused deadlock situation appearance so as to avoid due to accessing the thread of the protection zone.And the thread synchronization method realize with the state of a variable control all protection zones whether the state that can be accessed so that realize all protection zones state be in controllable state purpose.
Description
Technical field
The application is related to Computer Applied Technology field, same more specifically to a kind of thread synchronization method and thread
Step system.
Background technology
Multithreading operation, which can be realized, makes full use of system resource, shortens the program response time and improves the mesh of Consumer's Experience
, therefore be widely used in all kinds of computer programs.Thread synchronization is indispensable step in multithreading operation, is rationally had
The thread synchronization of effect can effectively ensure the normal operation of the correctness and program of data.
Due to there are some sensitive datas in systems, these sensitive datas do not allow to be accessed simultaneously by multiple threads, this
When be accomplished by multithreading carry out thread synchronization, to ensure the guarantor for including sensitive data in same time only one of which thread accesses
Region is protected, so as to ensure the integrality of sensitive data.Thread synchronization refers to the collaboration step between multithreading, i.e., between multithreading
Protection zone is accessed according to predetermined precedence.The method that multithreading carries out thread synchronization is mainly included in the prior art
" critical zone method " " event method " " mutex method " and " semaphore method ", but in actual application, these thread synchronization sides
Method may all be present when the thread for accessing protection zone be killed, caused by the protection zone feelings that cannot discharge or reset
Condition occurs, and in this case, other threads can not access protection zone, cause deadlock.
The content of the invention
In order to solve the above technical problems, the invention provides a kind of thread synchronization method and thread synchronization system, to realize
Avoid accessing the deadlock situation that the thread of protection zone is killed and is likely to occur due to working as.
To realize above-mentioned technical purpose, the embodiments of the invention provide following technical scheme:
A kind of thread synchronization method, including:
Global variable is defined, the global variable includes the first variable and the second variable, and first variable includes first
State and the second state, second variable are used to carry out locking operation to first variable;
The newly-built mutual exclusion lock class object in each protection zone, class includes destructor function and pointer member variable, the finger
Pin member variable is used to preserve first variable and the second variable;
By first variable and the second variable with the structure of the incoming all mutual exclusion lock class objects of pointer parameter form
Make in function, first variable includes first state and the second state, the first variable status is described for identifying
Protection zone whether can be by thread accesses;
When the thread request accesses any protection zone, according to judging the first variable status
Whether protection zone can be accessed, and the protection zone is accessed when the protection zone can be accessed, and after accessing successfully
The first variable status is changed in the destructor function so that the protection zone can be accessed.
Optionally, when first variable is in first state, all protection zones are in can be by thread accesses
State, when first variable is in the second state, all protection zones are in can not be by thread accesses state.
Optionally, it is described when the thread request accesses any protection zone, according to residing for first variable
Whether protection zone described in condition adjudgement can be accessed, and the protection zone is accessed when the protection zone can be accessed, and
The first variable status is being changed after accessing successfully in the destructor function so that the protection zone can be accessed
Including:
When the thread request accesses any protection zone, the first variable status is obtained, institute is judged
State whether the first variable states are in first state, if it is, accessing the protection zone, and utilize second variable pair
First variable carries out locking operation, and the state of first variable is changed into the second state, when the thread accesses into
The state of first variable is changed to first state in the destructor function after work(;If it is not, then judging access time
It is whether overtime, if it is not, then returning to the step of obtaining the first variable status after preset time.
Optionally, acquisition the first variable status includes:
The first variable status is obtained using query function.
A kind of thread synchronization system, including:
Variable-definition module, for defining global variable, the global variable includes the first variable and the second variable, described
First variable includes first state and the second state, and second variable is used to carry out locking operation to first variable;
Class definition module, for the newly-built mutual exclusion lock class object in each protection zone, class includes constructed fuction, destructed letter
Count and for preserving first variable and bivariate pointer member variable;
Afferent module, for by first variable and the second variable with the incoming all mutual exclusions of pointer parameter form
In the constructed fuction for locking class object, first variable includes first state and the second state, the first variable status
For identifying the protection zone whether can be by thread accesses;
Judge module, for when the thread request accesses any protection zone, according to the first variable institute
Whether protection zone described in place's condition adjudgement can be accessed, and the protection zone is accessed when the protection zone can be accessed,
And the first variable status is being changed after accessing successfully in the destructor function so that the protection zone can be interviewed
Ask.
Optionally, when first variable is in first state, all protection zones are in can be by thread accesses
State, when first variable is in the second state, all protection zones are in can not be by thread accesses state.
Optionally, first variable is Boolean type variable, and the first state is False, and the second state is True;
Second variable is thread lock variable.
Optionally, the judge module is specifically for when the thread request accesses any protection zone, obtaining
The first variable status, judges whether first variable states are in first state, if it is, accessing described protect
Region is protected, and locking operation is carried out to first variable using second variable, and by the state of first variable more
The second state is changed to, the state of first variable is changed to first in the destructor function after thread accesses success
State;If it is not, then judging whether access time is overtime, the first variable institute is obtained if it is not, then being returned after preset time
The step of place's state.
Optionally, the judge module obtains the first variable status specifically for being obtained using query function
The first variable status.
It can be seen from the above technical proposal that the embodiments of the invention provide a kind of thread synchronization method and thread synchronization system
System, wherein, the thread synchronization method is ensured after any protection zone success of thread accesses by destructor function in destructed letter
The first variable status is changed in number so that protection zone can be accessed, to discharge in thread access process for protection zone
The locking in domain, so as to realize the purpose for discharging the protection zone in time;And it ought access the line of the protection zone
When journey is killed, the normal operation of destructor function is not interfered with yet, so as to avoid due to accessing the protection zone
Thread is killed and possible caused deadlock situation occurs.
And the thread synchronization method reflects the accessiable state of protection zone by the first variable status, lead to
Cross and define the second variable to lock to protect the first variable, and the first variable and the second variable are with pointer parameter
The incoming all mutual exclusion lock class objects of form constructed fuction in, realization all protection zones are controlled with the state of a variable
Whether the state that can be accessed so that realize all protection zones state be in controllable state purpose.
Brief description of the drawings
In order to illustrate more clearly about the embodiment of the present invention or technical scheme of the prior art, below will be to embodiment or existing
There is the accompanying drawing used required in technology description to be briefly described, it should be apparent that, drawings in the following description are only this
The embodiment of invention, for those of ordinary skill in the art, on the premise of not paying creative work, can also basis
The accompanying drawing of offer obtains other accompanying drawings.
A kind of schematic flow sheet for thread synchronization method that Fig. 1 provides for one embodiment of the application;
A kind of schematic flow sheet for thread synchronization method that Fig. 2 provides for another embodiment of the application;
A kind of schematic flow sheet for thread synchronization method that Fig. 3 provides for another embodiment of the application;
A kind of structural representation for thread synchronization system that Fig. 4 provides for the application one embodiment.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present invention, the technical scheme in the embodiment of the present invention is carried out clear, complete
Site preparation is described, it is clear that described embodiment is only a part of embodiment of the invention, rather than whole embodiments.It is based on
Embodiment in the present invention, it is every other that those of ordinary skill in the art are obtained under the premise of creative work is not made
Embodiment, belongs to the scope of protection of the invention.
The embodiment of the present application provides a kind of thread synchronization method, as shown in figure 1, including:
S101:Global variable is defined, global variable includes the first variable and the second variable, and the first variable includes first state
With the second state, the second variable is used to carry out locking operation to the first variable;
S102:The newly-built mutual exclusion lock class object in each protection zone, class includes destructor function and pointer member variable, refers to
Pin member variable is used to preserve the first variable and the second variable;
S103:By the first variable and the second variable with the construction letter of the incoming all mutual exclusion lock class objects of pointer parameter form
In number, the first variable includes first state and the second state, and whether the first variable status is used to identify protection zone can quilt
Thread accesses;
S104:When thread request accesses any protection zone, judge that protection zone is according to the first variable status
It is no to be accessed, protection zone is accessed when protection zone can be accessed, and changing after accessing successfully in destructor function
One variable status is so that protection zone can be accessed;
It should be noted that in the prior art, when a thread enters a protection zone, compiler or actuator
Automatically generate a temporary variable to be used for locking the protection zone, other threads, which desire access to the protection zone, to be blocked,
The protection zone unlocked by temporary variable until accessing before when the thread accesses of the protection zone are finished and left, other threads
The protection zone can just be accessed.
In this application, the structure of the first variable and the second variable incoming all mutual exclusion lock class objects in the form of pointer parameter
After making in function, the constructed fuction of each mutual exclusion lock class object will carry out assignment operation, and the first variable and the second variable is equal
The first argument pointer member variable and the second argument pointer member variable defined in mutual exclusion lock class object are assigned to, completes described
Pointer member variable preserves first variable and bivariate operation.First variable is used for the access shape for identifying protection zone
Whether state, the protection zone is judged by the status of the first variable when a certain thread desires access to any protection zone
It can be accessed, the first variable is locked using the second variable in access process, the locking is operated with facing in the prior art
Locking operation of the variations per hour to protection zone is identical, and the application will not be described here.Second variable is locked to the first variable
The purpose of operation is that the state for avoiding the first variable during the thread accesses changes, and is changed after the completion of thread accesses
The state of first variable, operated equivalent to being realized using the second variable to the locking of protection zone and unblock, it is to avoid in thread
Deadlock situation caused by being killed during accessing protection zone.
Mutual exclusion lock class object is carrier of the thread synchronization method in each protection zone, to ensure that thread synchronization method exists
Execution in each protection zone;In the other embodiment of the application, class also includes being used to inquire about looking into for the first variable states
Ask function.
On the basis of above-described embodiment, in the specific embodiment of the application, when the first variable is in the first shape
During state, all protection zones are in can be by thread accesses state, when the first variable is in the second state, at all protection zones
In can not be by thread accesses state.
As shown in Fig. 2 when thread request accesses any protection zone, protection zone is judged according to the first variable status
Whether domain can be accessed, and access protection zone when protection zone can be accessed, and after accessing successfully in destructor function more
Change the first variable status so that protection zone can be accessed including:
S1041:When thread request accesses any protection zone, the first variable status is obtained, the first variable is judged
Whether state is in first state, if it is, accessing protection zone, and carries out locking behaviour to the first variable using the second variable
Make, and the state of the first variable is changed to the second state, by the first variable in destructor function after thread accesses success
State be changed to first state;If it is not, then judging whether access time is overtime, obtained if it is not, then being returned after preset time
The step of taking the first variable status.
Generally, a program has a main thread and multiple tasks thread, wherein, main thread is often with interface
Operation is related, and by taking Windows systems as an example, in Windows median surfaces, thread is exactly main thread, and main thread can be with calling system
API (Application Programming Interface, application programming interface) function creation mission threads are performed
The task of distribution, such as performing one section of code.But during mission thread performs the task of distribution, may be because of
Some reasons (for example program is switched off suddenly) need terminated task thread, are at this moment accomplished by main thread and remove calling system API letters
Number turns off mission thread, and this main thread actively goes to close mission thread when the task of mission thread also is not carried out finishing
Operation, that is, be referred to as killing thread, for mission thread be exactly by the in the case of of killing.And appoint except main thread can be terminated
It is engaged in outside thread, can also operates mutually to terminate other side's thread between mission thread.In the prior art, if thread is being killed
Protected data are being accessed when falling, then the region comprising protected data is in the lock state, and because thread is killed
The region is not discharged when falling or reset process is so that it is in the lock state always so that other threads can not be accessed
Protection zone, causes the generation of deadlock situation, so as to influence the normal operation of program or system.
But in the present embodiment, thread synchronization method ensures any protection zone success of thread accesses by destructor function
The state of the first variable is changed to first state afterwards, to discharge the locking in thread access process for protection zone, from
And realize the purpose in timely release guard region;And, also will not shadow when the thread for accessing protection zone is killed
Ring the normal operation of destructor function, thus avoid due to access the thread of protection zone killed and may caused by it is dead
Phenomenon is locked to occur.
And thread synchronization method reflects the accessiable state of protection zone by the first variable status, by fixed
Adopted second variable is locked to protect to the first variable, and the first variable and the second variable are with the shape of pointer parameter
In the constructed fuction of the incoming all mutual exclusion lock class objects of formula, realization controls all mutual exclusion lock class objects with the state of a variable
Whether the state that can be accessed so that the state of all protection zones be in controllable state, protection zone region compared with
In the case of big, it can efficiently solve because the coupling of common variable between protection zone causes resource contention fierce and causes
Efficiency decline problem.
It should also be noted that, generally the first variable and the second variable is incoming all in pointer parameter form
, it is necessary to be initialized to the first variable and bivariate value, to ensure thread synchronization method before in mutual exclusion lock class object
Normal operation.
In addition, timing is carried out when thread initiates the access request to protection zone as access time, when
When the protection zone of mutual exclusion lock class object locking is in inaccessible state, the thread reacquires first every preset time
Variable status is to judge whether protection zone is in accessiable state, if it is, being conducted interviews immediately to protection zone, such as
It is really no, then judge whether access time exceedes defined time-out time, if access time exceedes time-out time, assert time-out,
The access to protection zone is abandoned, if access time is not less than time-out time, again waits for returning after preset time and obtains the
One variable status.Time-out time can be set according to actual conditions, for example, can be 500ms, can also be 600ms or
1000ms etc., the value of preset time can be 10ms or 5ms, can also be 15ms.The application is to preset time
Specific value is not limited, specifically depending on actual conditions.
On the basis of above-described embodiment, in another embodiment of the application, as shown in figure 3, obtaining the first variable
State includes:
The first variable status is obtained using query function.
On the basis of above-described embodiment, in another embodiment of the application, the first variable is Boolean type variable, the
One state is False, and the second state is True;
Second variable is thread lock variable.
Because Boolean type variable only has two values, i.e. True or False, two shapes that can well with the first variable
State is corresponding.But in the other embodiment of the application, the first variable can also be other kinds of variable, the application to this simultaneously
Do not limit, specifically depending on actual conditions.
So in the present embodiment, accordingly, locking operation is carried out to the first variable using the second variable, and first is become
The state of amount, which is changed to the second state, to be included:
Locking operation is carried out to the first variable using thread lock variable, to protect the first variable;
The state of first variable is changed to True.
In summary, the embodiment of the present application provides a kind of thread synchronization method, and the thread synchronization method passes through destructed letter
Count that the state of the first variable is changed into first state after ensureing any protection zone success of thread accesses, to discharge in thread
For the locking of protection zone in access process, so as to realize the purpose in timely release guard region;And ought it access
When the thread of protection zone is killed, the normal operation of destructor function is not interfered with yet, so as to avoid due to accessing guarantor
The thread in shield region is killed and possible caused deadlock situation occurs.
And thread synchronization method reflects the accessiable state of protection zone by the first variable status, by fixed
Adopted second variable is locked to protect to the first variable, and the first variable and the second variable are with the shape of pointer parameter
In the constructed fuction of the incoming all mutual exclusion lock class objects of formula, whether realization controls all protection zones with the state of a variable
The state that can be accessed, so as to realize that the state of all protection zones is in the purpose of controllable state.
Accordingly, the embodiment of the present application additionally provides a kind of thread synchronization system, as shown in figure 4, including:
Variable-definition module 100, for defining global variable, global variable includes the first variable and the second variable, first
Variable includes first state and the second state, and the second variable is used to carry out locking operation to the first variable;
Class definition module 200, for the newly-built mutual exclusion lock class object in each protection zone, class includes constructed fuction, analysis
Structure function and for preserving the first variable and bivariate pointer member variable;
Afferent module 300, for by the first variable and the second variable with the incoming all mutual exclusion lock classes of pointer parameter form
In the constructed fuction of object, the first variable includes first state and the second state, and the first variable status, which is used to identify, to be protected
Region whether can be by thread accesses;;
Judge module 400, for when thread request accesses any protection zone, being judged according to the first variable status
Whether protection zone can be accessed, and protection zone is accessed when protection zone can be accessed, and in destructed letter after accessing successfully
The first variable status is changed in number so that protection zone can be accessed.
It should be noted that in the prior art, when a thread enters a protection zone, compiler or actuator
Automatically generate a temporary variable to be used for locking the protection zone, other threads, which desire access to the protection zone, to be blocked,
The protection zone unlocked by temporary variable until accessing before when the thread accesses of the protection zone are finished and left, other threads
The protection zone can just be accessed.
In this application, the structure of the first variable and the second variable incoming all mutual exclusion lock class objects in the form of pointer parameter
After making in function, the constructed fuction of each mutual exclusion lock class object will carry out assignment operation, and the first variable and the second variable is equal
The first argument pointer member variable and the second argument pointer member variable defined in mutual exclusion lock class object are assigned to, completes described
Pointer member variable preserves first variable and bivariate operation.First variable is used for the access shape for identifying protection zone
Whether state, the protection zone is judged by the status of the first variable when a certain thread desires access to any protection zone
It can be accessed, the first variable is locked using the second variable in access process, the locking is operated with facing in the prior art
Locking operation of the variations per hour to protection zone is identical, and the application will not be described here.Second variable is locked to the first variable
The purpose of operation is that the state for avoiding the first variable during the thread accesses changes, and is changed after the completion of thread accesses
The state of first variable, operated equivalent to being realized using the second variable to the locking of protection zone and unblock, it is to avoid in thread
Deadlock situation caused by being killed during accessing protection zone.
Mutual exclusion lock class object is carrier of the method in each protection zone performed by thread synchronization system, to ensure line
Execution of the method in each protection zone performed by journey synchronization system;In the other embodiment of the application, class also includes
Query function for inquiring about the first variable states.
On the basis of above-described embodiment, in the specific embodiment of the application, when the first variable is in the first shape
During state, all protection zones are in can be by thread accesses state, when the first variable is in the second state, at all protection zones
In can not be by thread accesses state.
Judge module is specifically for when thread request accesses any protection zone, obtaining the first variable status, sentencing
Whether disconnected first variable states are in first state, if it is, protection zone is accessed, and using the second variable to the first variable
Locking operation is carried out, and the state of the first variable is changed to the second state, after thread accesses success in destructor function
The state of first variable is changed to first state;If it is not, then judge whether access time is overtime, if it is not, then when default
Between after return obtain the first variable status the step of.
Generally, a program has a main thread and multiple tasks thread, wherein, main thread is often with interface
Operation is related, and by taking Windows systems as an example, in Windows median surfaces, thread is exactly main thread, and main thread can be with calling system
API (Application Programming Interface, application programming interface) function creation mission threads are performed
The task of distribution, such as performing one section of code.But during mission thread performs the task of distribution, may be because of
Some reasons (for example program is switched off suddenly) need terminated task thread, are at this moment accomplished by main thread and remove calling system API letters
Number turns off mission thread, and this main thread actively goes to close mission thread when the task of mission thread also is not carried out finishing
Operation, that is, be referred to as killing thread, for mission thread be exactly by the in the case of of killing.And appoint except main thread can be terminated
It is engaged in outside thread, can also operates mutually to terminate other side's thread between mission thread.In the prior art, if thread is being killed
Protected data are being accessed when falling, then the region comprising protected data is in the lock state, and because thread is killed
The region is not discharged when falling or reset process is so that it is in the lock state always so that other threads can not be accessed
Protection zone, causes the generation of deadlock situation, so as to influence the normal operation of program or system.
But in the present embodiment, thread synchronization system ensures any protection zone success of thread accesses by destructor function
The state of the first variable is changed to first state afterwards, to discharge the locking in thread access process for protection zone, from
And realize the purpose in timely release guard region;And, also will not shadow when the thread for accessing protection zone is killed
Ring the normal operation of destructor function, thus avoid due to access the thread of protection zone killed and may caused by it is dead
Phenomenon is locked to occur.
And thread synchronization method reflects the accessiable state of protection zone by the first variable status, by fixed
Adopted second variable is locked to protect to the first variable, and the first variable and the second variable are with the shape of pointer parameter
In the constructed fuction of the incoming all mutual exclusion lock class objects of formula, realization controls all mutual exclusion lock class objects with the state of a variable
Whether the state that can be accessed so that the state of all protection zones be in controllable state, protection zone region compared with
In the case of big, it can efficiently solve because the coupling of common variable between protection zone causes resource contention fierce and causes
Efficiency decline problem.
It should also be noted that, generally the first variable and the second variable is incoming all in pointer parameter form
, it is necessary to be initialized to the first variable and bivariate value, to ensure thread synchronization system before in mutual exclusion lock class object
Normal operation.
In addition, when carrying out timing as accessing when thread initiates the access request to mutual exclusion lock class object
Between, when the protection zone that mutual exclusion lock class object is locked is in inaccessible state, the thread is obtained again every preset time
Take the first variable status to judge whether protection zone is in accessiable state, if it is, immediately carrying out protection zone
Access, if it is not, then judging whether access time exceedes defined time-out time, if access time exceedes time-out time, recognize
Fixed time-out, abandons the access to protection zone, if access time is not less than time-out time, again waits for returning after preset time
Obtain the first variable status.Time-out time can be set according to actual conditions, for example, can be 500ms, can also be
600ms or 1000ms etc., the value of preset time can be 10ms or 5ms, can also be 15ms.The application is to pre-
If the specific value of time is not limited, specifically depending on actual conditions.
On the basis of above-described embodiment, in one embodiment of the application, judge module 400 obtains the first variable shape
State using query function specifically for obtaining the first variable status.
On the basis of above-described embodiment, in another embodiment of the application, the first variable is Boolean type variable, the
One state is False, and the second state is True;
Second variable is thread lock variable.
Because Boolean type variable only has two values, i.e. True or False, two shapes that can well with the first variable
State is corresponding.But in the other embodiment of the application, the first variable can also be other kinds of variable, the application to this simultaneously
Do not limit, specifically depending on actual conditions.
So in the present embodiment, accordingly, judge module 500 carries out locking behaviour using the second variable to the first variable
Make, and the state of the first variable is changed to the second state specifically for carrying out locking behaviour to the first variable using thread lock variable
Make, to protect the first variable;
The state of first variable is changed to True.
In summary, the embodiment of the present application provides a kind of thread synchronization method and thread synchronization system, wherein, thread is same
The state of first variable is changed to first by one step process after ensureing any protection zone success of thread accesses by destructor function
State, to discharge the locking in thread access process for protection zone, so as to realize the purpose in timely release guard region;
And when the thread for accessing protection zone is killed, the normal operation of destructor function is not interfered with yet, so as to avoid
Due to access the thread of protection zone killed and may caused by deadlock situation occur.
And thread synchronization method reflects the accessiable state of protection zone by the first variable status, by fixed
Adopted second variable is locked to protect to the first variable, and the first variable and the second variable are with the shape of pointer parameter
In the constructed fuction of the incoming all mutual exclusion lock class objects of formula, whether realization controls all protection zones with the state of a variable
The state that can be accessed, so as to realize that the state of all protection zones is in the purpose of controllable state.
The embodiment of each in this specification is described by the way of progressive, and what each embodiment was stressed is and other
Between the difference of embodiment, each embodiment identical similar portion mutually referring to.
The foregoing description of the disclosed embodiments, enables professional and technical personnel in the field to realize or using the present invention.
A variety of modifications to these embodiments will be apparent for those skilled in the art, as defined herein
General Principle can be realized in other embodiments without departing from the spirit or scope of the present invention.Therefore, it is of the invention
The embodiments shown herein is not intended to be limited to, and is to fit to and principles disclosed herein and features of novelty phase one
The most wide scope caused.
Claims (10)
1. a kind of thread synchronization method, it is characterised in that including:
Global variable is defined, the global variable includes the first variable and the second variable, and first variable includes first state
With the second state, second variable is used to carry out locking operation to first variable;
The newly-built mutual exclusion lock class object in each protection zone, class include destructor function and pointer member variable, the pointer into
Member's variable is used to preserve first variable and the second variable;
By first variable and the second variable with the construction letter of the incoming all mutual exclusion lock class objects of pointer parameter form
In number, first variable includes first state and the second state, and the first variable status is used to identify the protection
Region whether can be by thread accesses;
When the thread request accesses any protection zone, the protection is judged according to the first variable status
Whether region can be accessed, and the protection zone is accessed when the protection zone can be accessed, and in institute after accessing successfully
State and the first variable status is changed in destructor function so that the protection zone can be accessed.
2. thread synchronization method according to claim 1, it is characterised in that when first variable is in first state
When, all protection zones are in can be all described when first variable is in the second state by thread accesses state
Protection zone is in can not be by thread accesses state.
3. thread synchronization method according to claim 2, it is characterised in that first variable is Boolean type variable, institute
First state is stated for False, the second state is True;
Second variable is thread lock variable.
4. thread synchronization method according to claim 1, it is characterised in that described when the thread request accesses any institute
When stating protection zone, judge whether the protection zone can be accessed according to the first variable status, in the protection
Region accesses the protection zone when can be accessed, and is changing first variable after accessing successfully in the destructor function
Status so that the protection zone can be accessed including:
When the thread request accesses any protection zone, the first variable status is obtained, described the is judged
Whether one variable states are in first state, if it is, the protection zone is accessed, and using second variable to described
First variable carries out locking operation, and the state of first variable is changed into the second state, after thread accesses success
The state of first variable is changed to first state in the destructor function;If it is not, then whether judging access time
Time-out, if it is not, then returning to the step of obtaining the first variable status after preset time.
5. thread synchronization method according to claim 4, it is characterised in that acquisition the first variable status
Including:
The first variable status is obtained using query function.
6. a kind of thread synchronization system, it is characterised in that including:
Variable-definition module, for defining global variable, the global variable includes the first variable and the second variable, described first
Variable includes first state and the second state, and second variable is used to carry out locking operation to first variable;
Class definition module, for the newly-built mutual exclusion lock class object in each protection zone, class include constructed fuction, destructor function and
For preserving first variable and bivariate pointer member variable;
Afferent module, for by first variable and the second variable with the incoming all mutual exclusion lock classes of pointer parameter form
In the constructed fuction of object, first variable includes first state and the second state, and the first variable status is used for
Whether identify the protection zone can be by thread accesses;
Judge module, for when the thread request access any protection zone when, the shape according to residing for first variable
State judges whether the protection zone can be accessed, and accesses the protection zone when the protection zone can be accessed, and
The first variable status is changed so that the protection zone can be accessed in the destructor function after accessing successfully.
7. thread synchronization system according to claim 6, it is characterised in that when first variable is in first state
When, all protection zones are in can be all described when first variable is in the second state by thread accesses state
Protection zone is in can not be by thread accesses state.
8. thread synchronization system according to claim 7, it is characterised in that first variable is Boolean type variable, institute
First state is stated for False, the second state is True;
Second variable is thread lock variable.
9. thread synchronization system according to claim 6, it is characterised in that the judge module is specifically for when described
When thread request accesses any protection zone, the first variable status is obtained, first variable states are judged
Whether first state is in, if it is, accessing the protection zone, and first variable is entered using second variable
Row locks operation, and the state of first variable is changed into the second state, described destructed after thread accesses success
The state of first variable is changed to first state in function;If it is not, then judge whether access time is overtime, if it is not,
The step of obtaining the first variable status is then returned after preset time.
10. thread synchronization system according to claim 9, it is characterised in that the judge module obtains described first and become
Status is measured specifically for obtaining the first variable status using query function.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201710208287.0A CN106980544B (en) | 2017-03-31 | 2017-03-31 | Thread synchronization method and thread synchronization system |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201710208287.0A CN106980544B (en) | 2017-03-31 | 2017-03-31 | Thread synchronization method and thread synchronization system |
Publications (2)
Publication Number | Publication Date |
---|---|
CN106980544A true CN106980544A (en) | 2017-07-25 |
CN106980544B CN106980544B (en) | 2020-03-03 |
Family
ID=59339472
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201710208287.0A Active CN106980544B (en) | 2017-03-31 | 2017-03-31 | Thread synchronization method and thread synchronization system |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN106980544B (en) |
Cited By (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108009429A (en) * | 2017-12-11 | 2018-05-08 | 北京奇虎科技有限公司 | A kind of patch function generation method and device |
CN108762941A (en) * | 2018-04-12 | 2018-11-06 | 武汉斗鱼网络科技有限公司 | Multi-threaded Access Methods and device |
CN109032818A (en) * | 2018-07-27 | 2018-12-18 | 北京计算机技术及应用研究所 | A kind of method of isomorphism system internuclear synchronization and communication |
CN109254834A (en) * | 2017-07-13 | 2019-01-22 | 普天信息技术有限公司 | A kind of multithreading starting synchronous method |
CN110221923A (en) * | 2019-06-06 | 2019-09-10 | 北京字节跳动网络技术有限公司 | Data access method, device and equipment |
CN110245006A (en) * | 2019-05-07 | 2019-09-17 | 深圳壹账通智能科技有限公司 | Processing method, device, equipment and the storage medium of block chain affairs |
CN111858074A (en) * | 2020-06-24 | 2020-10-30 | 深圳英飞拓智能技术有限公司 | Resource access method, device and equipment |
CN112631802A (en) * | 2019-04-29 | 2021-04-09 | 杭州涂鸦信息技术有限公司 | Inter-thread communication method and related device |
Citations (14)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040117793A1 (en) * | 2002-12-17 | 2004-06-17 | Sun Microsystems, Inc. | Operating system architecture employing synchronous tasks |
CN1983186A (en) * | 2005-12-12 | 2007-06-20 | 中兴通讯股份有限公司 | System for setting up scheduling restricted zone in built-in realtime system by process |
US20090240931A1 (en) * | 2008-03-24 | 2009-09-24 | Coon Brett W | Indirect Function Call Instructions in a Synchronous Parallel Thread Processor |
CN101685406A (en) * | 2008-09-27 | 2010-03-31 | 国际商业机器公司 | Method and system for operating instance of data structure |
CN101807157A (en) * | 2010-03-30 | 2010-08-18 | 南京恩瑞特实业有限公司 | Defensive programming method based on function access global variables |
CN101840351A (en) * | 2010-04-20 | 2010-09-22 | 深圳市融创天下科技发展有限公司 | Automatic mutex operation method based on multiple platforms |
CN101061462B (en) * | 2004-11-26 | 2010-10-06 | 国际商业机器公司 | Multiprocessor system and exclusive control method therein |
CN101635006B (en) * | 2008-07-22 | 2012-02-29 | 中国科学院计算技术研究所 | Mutual exclusion and semaphore cell block of multi-core processor satisfying SystemC syntax |
CN101872317B (en) * | 2010-07-16 | 2012-12-26 | 山东中创软件工程股份有限公司 | VxWorks multitask synchronization and communication method |
CN103207809A (en) * | 2012-01-13 | 2013-07-17 | 金蝶软件(中国)有限公司 | Method and system for removing mutual exclusive lock |
CN104220989A (en) * | 2011-12-21 | 2014-12-17 | 英特尔公司 | Methods and systems to identify and reproduce concurrency violations in multi-threaded programs |
CN102999378B (en) * | 2012-12-03 | 2015-08-26 | 中国科学院软件研究所 | A kind of read-write lock implement method |
CN105511969A (en) * | 2015-11-25 | 2016-04-20 | 中国船舶工业***工程研究院 | Inter-process thread mutex method |
CN106250244A (en) * | 2016-07-15 | 2016-12-21 | 北京金山安全软件有限公司 | Method and device for releasing mutual exclusion lock and electronic equipment |
-
2017
- 2017-03-31 CN CN201710208287.0A patent/CN106980544B/en active Active
Patent Citations (14)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040117793A1 (en) * | 2002-12-17 | 2004-06-17 | Sun Microsystems, Inc. | Operating system architecture employing synchronous tasks |
CN101061462B (en) * | 2004-11-26 | 2010-10-06 | 国际商业机器公司 | Multiprocessor system and exclusive control method therein |
CN1983186A (en) * | 2005-12-12 | 2007-06-20 | 中兴通讯股份有限公司 | System for setting up scheduling restricted zone in built-in realtime system by process |
US20090240931A1 (en) * | 2008-03-24 | 2009-09-24 | Coon Brett W | Indirect Function Call Instructions in a Synchronous Parallel Thread Processor |
CN101635006B (en) * | 2008-07-22 | 2012-02-29 | 中国科学院计算技术研究所 | Mutual exclusion and semaphore cell block of multi-core processor satisfying SystemC syntax |
CN101685406A (en) * | 2008-09-27 | 2010-03-31 | 国际商业机器公司 | Method and system for operating instance of data structure |
CN101807157A (en) * | 2010-03-30 | 2010-08-18 | 南京恩瑞特实业有限公司 | Defensive programming method based on function access global variables |
CN101840351A (en) * | 2010-04-20 | 2010-09-22 | 深圳市融创天下科技发展有限公司 | Automatic mutex operation method based on multiple platforms |
CN101872317B (en) * | 2010-07-16 | 2012-12-26 | 山东中创软件工程股份有限公司 | VxWorks multitask synchronization and communication method |
CN104220989A (en) * | 2011-12-21 | 2014-12-17 | 英特尔公司 | Methods and systems to identify and reproduce concurrency violations in multi-threaded programs |
CN103207809A (en) * | 2012-01-13 | 2013-07-17 | 金蝶软件(中国)有限公司 | Method and system for removing mutual exclusive lock |
CN102999378B (en) * | 2012-12-03 | 2015-08-26 | 中国科学院软件研究所 | A kind of read-write lock implement method |
CN105511969A (en) * | 2015-11-25 | 2016-04-20 | 中国船舶工业***工程研究院 | Inter-process thread mutex method |
CN106250244A (en) * | 2016-07-15 | 2016-12-21 | 北京金山安全软件有限公司 | Method and device for releasing mutual exclusion lock and electronic equipment |
Cited By (12)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109254834A (en) * | 2017-07-13 | 2019-01-22 | 普天信息技术有限公司 | A kind of multithreading starting synchronous method |
CN109254834B (en) * | 2017-07-13 | 2021-05-14 | 普天信息技术有限公司 | Multithreading starting synchronization method |
CN108009429A (en) * | 2017-12-11 | 2018-05-08 | 北京奇虎科技有限公司 | A kind of patch function generation method and device |
CN108009429B (en) * | 2017-12-11 | 2021-09-03 | 北京奇虎科技有限公司 | Patch function generation method and device |
CN108762941A (en) * | 2018-04-12 | 2018-11-06 | 武汉斗鱼网络科技有限公司 | Multi-threaded Access Methods and device |
CN109032818A (en) * | 2018-07-27 | 2018-12-18 | 北京计算机技术及应用研究所 | A kind of method of isomorphism system internuclear synchronization and communication |
CN112631802A (en) * | 2019-04-29 | 2021-04-09 | 杭州涂鸦信息技术有限公司 | Inter-thread communication method and related device |
CN112631802B (en) * | 2019-04-29 | 2024-04-12 | 杭州涂鸦信息技术有限公司 | Inter-thread communication method and related device |
CN110245006A (en) * | 2019-05-07 | 2019-09-17 | 深圳壹账通智能科技有限公司 | Processing method, device, equipment and the storage medium of block chain affairs |
CN110221923A (en) * | 2019-06-06 | 2019-09-10 | 北京字节跳动网络技术有限公司 | Data access method, device and equipment |
CN110221923B (en) * | 2019-06-06 | 2021-06-08 | 北京字节跳动网络技术有限公司 | Data access method, device and equipment |
CN111858074A (en) * | 2020-06-24 | 2020-10-30 | 深圳英飞拓智能技术有限公司 | Resource access method, device and equipment |
Also Published As
Publication number | Publication date |
---|---|
CN106980544B (en) | 2020-03-03 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN106980544A (en) | A kind of thread synchronization method and thread synchronization system | |
DE102013022299B3 (en) | Protection of global registers in a multithreaded processor | |
DE60127857T2 (en) | SECURITY PROCEDURE WITH DETERMINISTIC REAL-TIME PERFORMANCE OF MULTITASK APPLICATIONS OF CONTROL AND COMMAND TYPE WITH ERROR CONTROL | |
US7574425B2 (en) | System and method for query management in a database management system | |
CN104866762B (en) | Security management program function | |
AU2016244128A1 (en) | Processing database transactions in a distributed computing system | |
Atluri et al. | Multilevel secure transaction processing | |
Atkinson et al. | Synchronization in actor systems | |
CN105138310A (en) | Concurrent access control method and system | |
US9164793B2 (en) | Prioritized lock requests to reduce blocking | |
DE102014002181A1 (en) | Dynamic sharing of resources | |
CN104298623B (en) | Electronic installation, operating system and access control method | |
CN104239147A (en) | Method and system for processing deadlock cycle | |
DE102014103139B4 (en) | Parallelized execution of single-core control software on multi-core vehicle control units | |
CN102096605A (en) | Multi-level resource management implementation method in embedded real-time operation system | |
Bang et al. | Robust performance of main memory data structures by configuration | |
Liu et al. | Deadlock and liveness characterization for a class of generalized Petri nets | |
Murillo et al. | Coordinated roles: Promoting re-usability of coordinated active objects using event notification protocols | |
Liu et al. | Petri net based model checking for the collaborative-ness of multiple processes systems | |
Bensalem et al. | Knowledge-based distributed conflict resolution for multiparty interactions and priorities | |
CN102214094A (en) | Executing operations via asynchronous programming model | |
Raheja et al. | Improving architecture-based self-adaptation using preemption | |
US20100031260A1 (en) | Object-Oriented Thread Abort Mechanism for Real Time C++ Software | |
Jayanti et al. | Read/write based fast-path transformation for FCFS mutual exclusion | |
Zhu et al. | Complex data collection in large-scale RFID systems |
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 | ||
TR01 | Transfer of patent right |
Effective date of registration: 20231121 Address after: 200335 room 205, floor 2, building 9, No. 365, Linhong Road, Changning District, Shanghai Patentee after: Shanghai iqiyi New Media Technology Co.,Ltd. Address before: 10 / F and 11 / F, iqiyi innovation building, No.2 Beiyi street, Haidian District, Beijing 100080 Patentee before: BEIJING QIYI CENTURY SCIENCE & TECHNOLOGY Co.,Ltd. |
|
TR01 | Transfer of patent right |