CN106980544B - Thread synchronization method and thread synchronization system - Google Patents

Thread synchronization method and thread synchronization system Download PDF

Info

Publication number
CN106980544B
CN106980544B CN201710208287.0A CN201710208287A CN106980544B CN 106980544 B CN106980544 B CN 106980544B CN 201710208287 A CN201710208287 A CN 201710208287A CN 106980544 B CN106980544 B CN 106980544B
Authority
CN
China
Prior art keywords
variable
state
thread
protection area
access
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201710208287.0A
Other languages
Chinese (zh)
Other versions
CN106980544A (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.)
Shanghai IQIYI New Media Technology Co Ltd
Original Assignee
Beijing QIYI Century Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing QIYI Century Science and Technology Co Ltd filed Critical Beijing QIYI Century Science and Technology Co Ltd
Priority to CN201710208287.0A priority Critical patent/CN106980544B/en
Publication of CN106980544A publication Critical patent/CN106980544A/en
Application granted granted Critical
Publication of CN106980544B publication Critical patent/CN106980544B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual 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

The application discloses a thread synchronization method and a thread synchronization system, wherein the thread synchronization method ensures that the state of a first variable is changed into a first state after a thread accesses any one protection area successfully through a destructor so as to release the locking of the protection area in the thread access process, thereby realizing the purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided. And the thread synchronization method realizes the purpose that the state of all the protection areas can be controlled by the state of one variable, thereby realizing the purpose that the state of all the protection areas is in a controllable state.

Description

Thread synchronization method and thread synchronization system
Technical Field
The present application relates to the field of computer application technologies, and in particular, to a thread synchronization method and a thread synchronization system.
Background
The multithreading operation can realize the purposes of fully utilizing system resources, shortening program response time and improving user experience, and is widely applied to various computer programs. Thread synchronization is an indispensable step in multi-thread operation, and reasonable and effective thread synchronization can effectively ensure the correctness of data and the normal running of a program.
Because some sensitive data exist in the system, the sensitive data are not allowed to be accessed by a plurality of threads at the same time, and at the moment, the threads of multiple threads need to be synchronized, so that only one thread accesses a protection area containing the sensitive data at the same time, and the integrity of the sensitive data is ensured. Thread synchronization refers to the coordinated pacing between multiple threads, i.e., multiple threads access the protected area in a predetermined order. In the prior art, methods for thread synchronization of multiple threads mainly include a critical section method, an event method, a mutual exclusion method and a semaphore method, but in an actual application process, the thread synchronization methods may exist that when a thread accessing a protection area is killed, the protection area cannot be released or reset, and in this case, other threads cannot access the protection area, so that deadlock is caused.
Disclosure of Invention
In order to solve the above technical problems, the present invention provides a thread synchronization method and a thread synchronization system, so as to avoid a deadlock phenomenon that may occur when a thread accessing a protection area is killed.
In order to achieve the technical purpose, the embodiment of the invention provides the following technical scheme:
a method of thread synchronization, comprising:
defining a global variable, wherein the global variable comprises a first variable and a second variable, the first variable comprises a first state and a second state, and the second variable is used for locking the first variable;
newly building a mutual exclusion lock class object in each protection area, wherein the class comprises a destructor and a pointer member variable, and the pointer member variable is used for storing the first variable and the second variable;
transmitting the first variable and the second variable into constructors of all the mutex lock objects in a pointer parameter mode, wherein the first variable comprises a first state and a second state, and the state of the first variable is used for identifying whether the protection area can be accessed by a thread;
when the thread requests to access any one protection area, judging whether the protection area can be accessed according to the state of the first variable, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor function after the access is successful so that the protection area can be accessed.
Optionally, when the first variable is in the first state, all the protection regions are in a state accessible to a thread, and when the first variable is in the second state, all the protection regions are in a state inaccessible to the thread.
Optionally, when the thread requests to access any one of the protection areas, determining whether the protection area can be accessed according to the state of the first variable, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor function after the access is successful so that the protection area can be accessed includes:
when the thread requests to access any one protection area, acquiring the state of the first variable, judging whether the state of the first variable is in a first state, if so, accessing the protection area, locking the first variable by using the second variable, changing the state of the first variable into a second state, and changing the state of the first variable into the first state in the destructor function after the thread is successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
Optionally, the obtaining the state of the first variable includes:
and acquiring the state of the first variable by using a query function.
A thread synchronization system comprising:
the variable definition module is used for defining global variables, wherein the global variables comprise a first variable and a second variable, the first variable comprises a first state and a second state, and the second variable is used for locking the first variable;
the class definition module is used for newly building a mutual exclusion lock class object in each protection area, wherein each class comprises a constructor, a destructor and a pointer member variable used for storing the first variable and the second variable;
the transmission module is used for transmitting the first variable and the second variable into the constructors of all the mutex lock objects in a pointer parameter mode, wherein the first variable comprises a first state and a second state, and the state of the first variable is used for identifying whether the protection area can be accessed by a thread;
and the judging module is used for judging whether the protection area can be accessed according to the state of the first variable when the thread requests to access any protection area, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor function after the access is successful so that the protection area can be accessed.
Optionally, when the first variable is in the first state, all the protection regions are in a state accessible to a thread, and when the first variable is in the second state, all the protection regions are in a state inaccessible to the thread.
Optionally, the first variable is a boolean variable, the first state is False, and the second state is True;
the second variable is a thread lock variable.
Optionally, the determining module is specifically configured to, when the thread requests to access any one of the protection regions, obtain a state of the first variable, determine whether the state of the first variable is in a first state, if so, access the protection region, perform a locking operation on the first variable by using the second variable, change the state of the first variable to the second state, and change the state of the first variable to the first state in the destructor function after the thread is successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
Optionally, the determining module obtains the state of the first variable specifically for obtaining the state of the first variable by using a query function.
It can be seen from the foregoing technical solutions that the embodiments of the present invention provide a thread synchronization method and a thread synchronization system, where the thread synchronization method ensures that, after a thread accesses any protection area successfully through a destructor, a state of a first variable is changed in the destructor so that the protection area can be accessed, so as to release locking on the protection area in a process of accessing the thread, thereby achieving a purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided.
And the thread synchronization method reflects the accessible state of the protection area through the state of the first variable, locks the first variable for protection by defining the second variable, and transmits the first variable and the second variable into the constructors of all the mutex lock class objects in the form of pointer parameters, so that the state of one variable is used for controlling whether all the protection areas are accessible, and the purpose that the states of all the protection areas are in a controllable state is realized.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the provided drawings without creative efforts.
Fig. 1 is a schematic flowchart of a thread synchronization method according to an embodiment of the present application;
fig. 2 is a schematic flowchart of a thread synchronization method according to another embodiment of the present application;
FIG. 3 is a flowchart illustrating a thread synchronization method according to another embodiment of the present application;
fig. 4 is a schematic structural diagram of a thread synchronization system according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
An embodiment of the present application provides a thread synchronization method, as shown in fig. 1, including:
s101: defining global variables, wherein the global variables comprise a first variable and a second variable, the first variable comprises a first state and a second state, and the second variable is used for locking the first variable;
s102: newly building a mutual exclusion lock class object in each protection area, wherein the class comprises a destructor and a pointer member variable, and the pointer member variable is used for storing a first variable and a second variable;
s103: transmitting a first variable and a second variable into constructors of all the mutex lock objects in a pointer parameter mode, wherein the first variable comprises a first state and a second state, and the state of the first variable is used for identifying whether a protection area can be accessed by a thread;
s104: when a thread requests to access any protection area, judging whether the protection area can be accessed according to the state of the first variable, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in a destructor function after the access is successful so that the protection area can be accessed;
it should be noted that, in the prior art, when a thread enters a protected area, a compiler or an executor automatically generates a temporary variable to lock the protected area, and other threads that want to access the protected area are blocked, and until the threads that access the protected area leave after finishing accessing, other threads can access the protected area by unlocking the protected area through the temporary variable.
In the application, after a first variable and a second variable are transmitted to the constructors of all the mutex lock objects in the form of pointer parameters, the constructor of each mutex lock object performs assignment operation, and assigns both the first variable and the second variable to a first variable pointer member variable and a second variable pointer member variable defined in the mutex lock object, thereby completing the operation of storing the first variable and the second variable by the pointer member variables. The first variable is used to identify an access state of the protection area, when a certain thread wants to access any protection area, whether the protection area can be accessed is determined according to the state of the first variable, the first variable is locked by using the second variable in the access process, and the locking operation is the same as the locking operation of the temporary variable on the protection area in the prior art, which is not described herein again. The purpose of locking the first variable by the second variable is to prevent the state of the first variable from changing in the process of accessing the thread, and to change the state of the first variable after the thread access is completed, which is equivalent to using the second variable to realize locking and unlocking operations on the protection area, thereby avoiding deadlock caused by killing the thread in the process of accessing the protection area.
The mutual exclusion lock object is a carrier of the thread synchronization method in each protection area so as to ensure the execution of the thread synchronization method in each protection area; in other embodiments of the present application, the class further includes a query function for querying a state of the first variable.
Based on the foregoing embodiments, in a specific embodiment of the present application, when the first variable is in the first state, all protection regions are in a state accessible to the thread, and when the first variable is in the second state, all protection regions are in a state inaccessible to the thread.
As shown in fig. 2, when a thread requests to access any protection area, determining whether the protection area can be accessed according to the state of the first variable, accessing the protection area when the protection area can be accessed, and after the access is successful, changing the state of the first variable in the destructor so that the protection area can be accessed includes:
s1041: when a thread requests to access any protection area, acquiring the state of a first variable, judging whether the state of the first variable is in a first state, if so, accessing the protection area, locking the first variable by using a second variable, changing the state of the first variable into the second state, and changing the state of the first variable into the first state in a destructor function after the thread is successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
Generally, a program has a main thread and a plurality of task threads, where the main thread is often related to an Interface operation, for example, a Windows system, where the Interface thread is the main thread, and the main thread may call an API (Application Programming Interface) function of the system to create a task thread to execute an assigned task, such as executing a piece of code. However, during the process of executing the assigned task by the task thread, it may be necessary to terminate the task thread for some reason (for example, the program is suddenly turned off), and then the main thread is required to call the system API function to turn off the task thread, and this main thread actively turns off the task thread when the task of the task thread has not been completed, that is, it is called thread killing, and it is the case that the task thread is killed. And besides the main thread can terminate the task thread, the task threads can also mutually operate to terminate the opposite thread. In the prior art, if a thread is accessing protected data when being killed, an area containing the protected data is in a locked state, and the area is not released or reset when the thread is killed, so that the area is always in the locked state, and other threads cannot access the protected area, which causes a deadlock phenomenon, thereby affecting the normal operation of a program or a system.
In this embodiment, however, the thread synchronization method changes the state of the first variable to the first state after the thread accesses any protection area successfully through the destructor function, so as to release the lock on the protection area in the process of the thread access, thereby achieving the purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided.
And the thread synchronization method reflects the accessible state of the protection area through the state of the first variable, locks the first variable for protection by defining the second variable, and transmits the first variable and the second variable into the constructors of all the mutex lock objects in the form of pointer parameters, so as to control whether all the mutex lock objects can be accessed or not by using the state of one variable, thereby enabling the states of all the protection areas to be in a controllable state.
It should be noted that, in general, before the first variable and the second variable are transmitted to all mutex class objects in the form of pointer parameters, values of the first variable and the second variable need to be initialized to ensure normal operation of the thread synchronization method.
In addition, when a thread initiates an access request to a protected area, timing is taken as access time, when the protected areas locked by the mutex lock object are all in an inaccessible state, the thread acquires the state of the first variable again at intervals of preset time to judge whether the protected areas are in the accessible state, if so, the protected areas are immediately accessed, if not, whether the access time exceeds the specified timeout time is judged, if the access time exceeds the timeout time, the access is determined to be overtime, the access to the protected areas is abandoned, and if the access time does not exceed the timeout time, the state of the first variable is acquired after waiting for the preset time again. The timeout time may be set according to actual conditions, and may be, for example, 500ms, 600ms, 1000ms, or the like, and a value of the preset time may be 10ms, 5ms, or 15 ms. The specific value of the preset time is not limited, and is determined according to the actual situation.
On the basis of the above embodiment, in another embodiment of the present application, as shown in fig. 3, the acquiring the first variable state includes:
and acquiring the state of the first variable by using the query function.
On the basis of the above embodiment, in yet another embodiment of the present application, the first variable is a boolean variable, the first state is False, and the second state is True;
the second variable is a thread lock variable.
Since the boolean variable has only two values, True or False, it corresponds well to the two states of the first variable. However, in other embodiments of the present application, the first variable may also be other types of variables, which are not limited in this application, depending on the actual situation.
In this embodiment, correspondingly, the locking the first variable by the second variable, and changing the state of the first variable to the second state includes:
locking the first variable by using the thread lock variable to protect the first variable;
the state of the first variable is changed to True.
To sum up, the embodiment of the present application provides a thread synchronization method, where the thread synchronization method ensures that a state of a first variable is changed to a first state after a thread accesses any protection area successfully through a destructor function, so as to release locking on the protection area in a thread access process, thereby achieving a purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided.
And the thread synchronization method reflects the accessible state of the protection area through the state of the first variable, locks the first variable for protection by defining the second variable, and transmits the first variable and the second variable into the constructors of all the mutex lock objects in the form of pointer parameters, so that the state of one variable is used for controlling whether all the protection areas are accessible, and the purpose that the states of all the protection areas are in a controllable state is realized.
Correspondingly, an embodiment of the present application further provides a thread synchronization system, as shown in fig. 4, including:
the variable definition module 100 is configured to define a global variable, where the global variable includes a first variable and a second variable, the first variable includes a first state and a second state, and the second variable is used to perform a locking operation on the first variable;
the class definition module 200 is configured to newly build a mutex class object in each protected area, where the class includes a constructor, a destructor, and a pointer member variable for storing a first variable and a second variable;
a transmitting module 300, configured to transmit a first variable and a second variable into constructors of all mutex lock objects in a pointer parameter form, where the first variable includes a first state and a second state, and the state where the first variable is located is used to identify whether a protection area is accessible by a thread; (ii) a
The judging module 400 is configured to, when the thread requests to access any protection area, judge whether the protection area can be accessed according to the state of the first variable, access the protection area when the protection area can be accessed, and change the state of the first variable in the destructor after the access is successful so that the protection area can be accessed.
It should be noted that, in the prior art, when a thread enters a protected area, a compiler or an executor automatically generates a temporary variable to lock the protected area, and other threads that want to access the protected area are blocked, and until the threads that access the protected area leave after finishing accessing, other threads can access the protected area by unlocking the protected area through the temporary variable.
In the application, after a first variable and a second variable are transmitted to the constructors of all the mutex lock objects in the form of pointer parameters, the constructor of each mutex lock object performs assignment operation, and assigns both the first variable and the second variable to a first variable pointer member variable and a second variable pointer member variable defined in the mutex lock object, thereby completing the operation of storing the first variable and the second variable by the pointer member variables. The first variable is used to identify an access state of the protection area, when a certain thread wants to access any protection area, whether the protection area can be accessed is determined according to the state of the first variable, the first variable is locked by using the second variable in the access process, and the locking operation is the same as the locking operation of the temporary variable on the protection area in the prior art, which is not described herein again. The purpose of locking the first variable by the second variable is to prevent the state of the first variable from changing in the process of accessing the thread, and to change the state of the first variable after the thread access is completed, which is equivalent to using the second variable to realize locking and unlocking operations on the protection area, thereby avoiding deadlock caused by killing the thread in the process of accessing the protection area.
The mutex lock object is a carrier of the method executed by the thread synchronization system in each protection area so as to ensure the execution of the method executed by the thread synchronization system in each protection area; in other embodiments of the present application, the class further includes a query function for querying a state of the first variable.
Based on the foregoing embodiments, in a specific embodiment of the present application, when the first variable is in the first state, all protection regions are in a state accessible to the thread, and when the first variable is in the second state, all protection regions are in a state inaccessible to the thread.
The judging module is specifically configured to, when a thread requests access to any one of the protection regions, acquire a state of a first variable, judge whether the state of the first variable is in a first state, if so, access the protection region, perform locking operation on the first variable by using a second variable, change the state of the first variable to the second state, and change the state of the first variable to the first state in a destructor function after the thread has successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
Generally, a program has a main thread and a plurality of task threads, where the main thread is often related to an Interface operation, for example, a Windows system, where the Interface thread is the main thread, and the main thread may call an API (Application Programming Interface) function of the system to create a task thread to execute an assigned task, such as executing a piece of code. However, during the process of executing the assigned task by the task thread, it may be necessary to terminate the task thread for some reason (for example, the program is suddenly turned off), and then the main thread is required to call the system API function to turn off the task thread, and this main thread actively turns off the task thread when the task of the task thread has not been completed, that is, it is called thread killing, and it is the case that the task thread is killed. And besides the main thread can terminate the task thread, the task threads can also mutually operate to terminate the opposite thread. In the prior art, if a thread is accessing protected data when being killed, an area containing the protected data is in a locked state, and the area is not released or reset when the thread is killed, so that the area is always in the locked state, and other threads cannot access the protected area, which causes a deadlock phenomenon, thereby affecting the normal operation of a program or a system.
In this embodiment, however, the thread synchronization system changes the state of the first variable into the first state after the thread accesses any protection area successfully through the destructor function, so as to release the lock on the protection area in the process of the thread access, thereby achieving the purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided.
And the thread synchronization method reflects the accessible state of the protection area through the state of the first variable, locks the first variable for protection by defining the second variable, and transmits the first variable and the second variable into the constructors of all the mutex lock objects in the form of pointer parameters, so as to control whether all the mutex lock objects can be accessed or not by using the state of one variable, thereby enabling the states of all the protection areas to be in a controllable state.
It should be noted that, in general, before the first variable and the second variable are transmitted to all mutex class objects in the form of pointer parameters, values of the first variable and the second variable need to be initialized to ensure normal operation of the thread synchronization system.
In addition, when a thread initiates an access request to a mutex lock object, timing is taken as access time, when all protection areas locked by the mutex lock object are in an inaccessible state, the thread acquires the state of a first variable again at intervals of preset time to judge whether the protection areas are in an accessible state, if so, the protection areas are immediately accessed, if not, the access time is judged whether exceeds the specified timeout time, if so, the access time is determined to be overtime, the access to the protection areas is abandoned, and if not, the state of the first variable is acquired after waiting for the preset time again. The timeout time may be set according to actual conditions, and may be, for example, 500ms, 600ms, 1000ms, or the like, and a value of the preset time may be 10ms, 5ms, or 15 ms. The specific value of the preset time is not limited, and is determined according to the actual situation.
On the basis of the foregoing embodiments, in an embodiment of the present application, the determining module 400 obtains the state of the first variable specifically for obtaining the state where the first variable is located by using a query function.
On the basis of the above embodiment, in yet another embodiment of the present application, the first variable is a boolean variable, the first state is False, and the second state is True;
the second variable is a thread lock variable.
Since the boolean variable has only two values, True or False, it corresponds well to the two states of the first variable. However, in other embodiments of the present application, the first variable may also be other types of variables, which are not limited in this application, depending on the actual situation.
In this embodiment, correspondingly, the determining module 500 performs a locking operation on the first variable by using the second variable, and changes the state of the first variable into the second state, which is specifically used for performing a locking operation on the first variable by using the thread lock variable to protect the first variable;
the state of the first variable is changed to True.
To sum up, the embodiment of the present application provides a thread synchronization method and a thread synchronization system, where the thread synchronization method ensures that the state of a first variable is changed into a first state after a thread accesses any protection area successfully through a destructor function, so as to release the lock on the protection area in the process of accessing the thread, thereby achieving the purpose of releasing the protection area in time; and when the thread accessing the protection area is killed, the normal operation of the destructor is not influenced, so that the deadlock phenomenon possibly caused by the fact that the thread accessing the protection area is killed is avoided.
And the thread synchronization method reflects the accessible state of the protection area through the state of the first variable, locks the first variable for protection by defining the second variable, and transmits the first variable and the second variable into the constructors of all the mutex lock objects in the form of pointer parameters, so that the state of one variable is used for controlling whether all the protection areas are accessible, and the purpose that the states of all the protection areas are in a controllable state is realized.
The embodiments in the present description are described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments are referred to each other.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (10)

1. A method for thread synchronization, comprising:
defining a global variable, wherein the global variable comprises a first variable and a second variable, the first variable comprises a first state and a second state, and the second variable is used for locking the first variable;
newly building a mutual exclusion lock class object in each protection area, wherein the class comprises a destructor and a pointer member variable, and the pointer member variable is used for storing the first variable and the second variable;
transmitting the first variable and the second variable into constructors of all the mutex lock objects in a pointer parameter mode, wherein the first variable comprises a first state and a second state, and the state of the first variable is used for identifying whether the protection area can be accessed by a thread;
when the thread requests to access any one protection area, judging whether the protection area can be accessed according to the state of the first variable, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor function after the access is successful so that the protection area can be accessed.
2. The thread synchronization method of claim 1, wherein all of the protection zones are in a state accessible to threads when the first variable is in a first state and are in a state inaccessible to threads when the first variable is in a second state.
3. The thread synchronization method of claim 2, wherein the first variable is a boolean variable, the first state is False, and the second state is True;
the second variable is a thread lock variable.
4. The method according to claim 1, wherein the determining whether the protection area can be accessed according to the state of the first variable when the thread requests to access any of the protection areas, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor after the access is successful so that the protection area can be accessed comprises:
when the thread requests to access any one protection area, acquiring the state of the first variable, judging whether the state of the first variable is in a first state, if so, accessing the protection area, locking the first variable by using the second variable, changing the state of the first variable into a second state, and changing the state of the first variable into the first state in the destructor function after the thread is successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
5. The method of claim 4, wherein obtaining the state of the first variable comprises:
and acquiring the state of the first variable by using a query function.
6. A thread synchronization system, comprising:
the variable definition module is used for defining global variables, wherein the global variables comprise a first variable and a second variable, the first variable comprises a first state and a second state, and the second variable is used for locking the first variable;
the class definition module is used for newly building a mutual exclusion lock class object in each protection area, wherein each class comprises a constructor, a destructor and a pointer member variable used for storing the first variable and the second variable;
the transmission module is used for transmitting the first variable and the second variable into the constructors of all the mutex lock objects in a pointer parameter mode, wherein the first variable comprises a first state and a second state, and the state of the first variable is used for identifying whether the protection area can be accessed by a thread;
and the judging module is used for judging whether the protection area can be accessed according to the state of the first variable when the thread requests to access any protection area, accessing the protection area when the protection area can be accessed, and changing the state of the first variable in the destructor function after the access is successful so that the protection area can be accessed.
7. The thread synchronization system of claim 6, wherein all of the protection zones are in a state accessible to threads when the first variable is in a first state and are in a state inaccessible to threads when the first variable is in a second state.
8. The thread synchronization system of claim 7, wherein the first variable is a boolean variable, the first state is False, and the second state is True;
the second variable is a thread lock variable.
9. The thread synchronization system according to claim 6, wherein the determining module is specifically configured to, when the thread requests to access any one of the protection regions, obtain a state of the first variable, determine whether the state of the first variable is in a first state, if so, access the protection region, perform a locking operation on the first variable by using the second variable, change the state of the first variable to the second state, and change the state of the first variable to the first state in the destructor function after the thread has successfully accessed; if not, judging whether the access time is overtime, and if not, returning to the step of acquiring the state of the first variable after the preset time.
10. The thread synchronization system according to claim 9, wherein the determining module obtains the state of the first variable, and is specifically configured to obtain the state of the first variable by using a query function.
CN201710208287.0A 2017-03-31 2017-03-31 Thread synchronization method and thread synchronization system Active CN106980544B (en)

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 CN106980544A (en) 2017-07-25
CN106980544B true 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)

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109254834B (en) * 2017-07-13 2021-05-14 普天信息技术有限公司 Multithreading starting synchronization method
CN108009429B (en) * 2017-12-11 2021-09-03 北京奇虎科技有限公司 Patch function generation method and device
CN108762941B (en) * 2018-04-12 2020-09-08 武汉斗鱼网络科技有限公司 Multithreading access method and device
CN109032818B (en) * 2018-07-27 2021-11-16 北京计算机技术及应用研究所 Method for synchronization and communication between cores of homogeneous system
CN110096378B (en) * 2019-04-29 2021-01-08 杭州涂鸦信息技术有限公司 Inter-thread communication method and related device
CN110245006B (en) * 2019-05-07 2023-05-02 深圳壹账通智能科技有限公司 Method, device, equipment and storage medium for processing block chain transaction
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

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1983186A (en) * 2005-12-12 2007-06-20 中兴通讯股份有限公司 System for setting up scheduling restricted zone in built-in realtime system by process
CN101685406A (en) * 2008-09-27 2010-03-31 国际商业机器公司 Method and system for operating instance of data structure
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
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

Family Cites Families (6)

* Cited by examiner, † Cited by third party
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
US8312254B2 (en) * 2008-03-24 2012-11-13 Nvidia Corporation Indirect function call instructions in a synchronous parallel thread processor
CN101807157B (en) * 2010-03-30 2012-08-29 南京恩瑞特实业有限公司 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
CN103207809B (en) * 2012-01-13 2017-07-07 金蝶软件(中国)有限公司 Remove the method and system of mutual exclusion lock
CN106250244B (en) * 2016-07-15 2019-12-24 珠海豹趣科技有限公司 Method and device for releasing mutual exclusion lock and electronic equipment

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
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
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
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
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

Also Published As

Publication number Publication date
CN106980544A (en) 2017-07-25

Similar Documents

Publication Publication Date Title
CN106980544B (en) Thread synchronization method and thread synchronization system
US8176489B2 (en) Use of rollback RCU with read-side modifications to RCU-protected data structures
US8327336B2 (en) Enhanced thread stepping
US8612982B2 (en) Multi-tasking method according to simple priority inheritance scheme and embedded system therefor
US8984245B2 (en) Memory protection unit and method for controlling an access to memory device
CN109918187B (en) Task scheduling method, device, equipment and storage medium
EP3037303B1 (en) Vehicle control device
CN104536834A (en) Method for authorizing lock permission and distributed lock manager
US7472389B2 (en) Stochastically based thread budget overrun handling system and method
JPH1115793A (en) Protection method for resource maintainability
CN112416556B (en) Data read-write priority balancing method, system, device and storage medium
Åsberg et al. Resource sharing using the rollback mechanism in hierarchically scheduled real-time open systems
US11115232B2 (en) Method and device for operating a control unit
CN107315631B (en) Task scheduling method and device
US9021483B2 (en) Making hardware objects and operations thread-safe
JP2010504581A (en) Robust critical section design in multithreaded applications
US20120054394A1 (en) Fast Biased Locks
Kalkov et al. Priority inheritance during remote procedure calls in real-time android using extended binder framework
CN112559132B (en) Safe static detection method and device for containerized deployment application
KR101207434B1 (en) System and Method for Preventing Collision Between Different Digital Documents Protection System
US6973590B1 (en) Terminating a child process without risk of data corruption to a shared resource for subsequent processes
CN113806099B (en) Binary calculation-based multi-core spin lock design method
CN106445976B (en) Database connection control method and device
CN112231131B (en) Method, device and equipment for realizing database lock and readable storage medium
JP2008077219A (en) Object-oriented vehicle control system

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