“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
What is a thread lock
In practice, resources should be avoided from being shared between threads to reduce interaction between threads. However, there are always multiple threads interfering with each other (such as multiple threads accessing a resource). In the case that threads must interact, it is necessary to ensure that only one thread accesses this resource at a time. Therefore, thread locks emerge as The Times require
The concept and classification of thread lock
-
A critical region
A piece of code that accesses a common resource is not a mechanism or algorithm
-
spinlocks
A lock used for multithreaded synchronization in which the thread repeatedly checks whether the lock variable is available. Because the thread keeps executing during this process, it is a busy wait. Once a spin lock is acquired, the thread holds it until it explicitly releases the spin lock. Spin-locks avoid the scheduling overhead of the process context and are therefore effective in situations where threads block only for short periods of time
-
The mutex
A mechanism used in multithreaded programming to prevent two threads from simultaneously reading or writing to the same common resource, such as a global variable. This is accomplished by slicing code into critical sections one by one
-
Read-write lock
A synchronization mechanism for concurrent control of computer programs, also known as shared-mutex, multireader-singwriter lock, used to solve the problem of multithreading reading and writing to a common resource. Read operations can be concurrently reentrant, and write operations are mutually exclusive. Read/write locks are usually implemented with mutex, condition variables, and semaphores
-
Semaphore
A more advanced synchronization mechanism, the mutex is a special case of semaphore with a value of 0/1 only. Semaphores can have more value space for more complex synchronization, rather than just mutual exclusion between threads
-
Conditions for the lock
A conditional variable that goes into hibernation, or locked, when certain resource requirements are not met. When the resource is allocated, the conditional lock is opened and the process continues
Thread lock performance
Find a graph of thread lock performance