SlideShare a Scribd company logo
1 of 16
LOCKS
THINH DANG AN
VU TRAN QUANG
THREAD-SAFE
• In multi-threaded programming, threads share resources.
• It may lead to race condition.
• A piece of code is thread-safe if it functions correctly during simultaneous
execution by multiple threads.
• Synchronization is the most widely used method to achieve thread-safe.
• Lock is a kind of synchronization.
REENTRANTLOCK
• A lock is acquired via lock() and released via unlock().
• If another thread has already acquired the lock, subsequent calls to lock() pause
the current thread until the lock has been unlocked.
• Only one thread can hold the lock at any given time.
• tryLock() is an alternative to lock().
READWRITELOCK
• This type of lock maintains a pair of locks for read and write access.
• Read-lock can be held simultaneously by multiple threads as long as no threads
hold the write-lock.
• Improve performance and throughput in situations that reads are more frequent
than writes.
READWRITELOCK
STAMPEDLOCK
• Also support read and write locks.
• locking methods return a stamp.
• These stamps are used to release a lock, to check if the lock is still valid or to
convert the mode.
• Main features of StampedLock compared to ReadWriteLock is mode conversion
and optimistic read.
STAMPEDLOCK
MODE CONVERSION
When and how to convert mode:
• You acquired the write lock and wrote something and you want to read in the
same critical section, use tryConvertToReadLock(long stamp).
• You acquired the read lock, and after a successful read, you wanted to change the
value, use tryConvertToWriteLock(long stamp).
STAMPEDLOCK
OPTIMISTIC READ
StampedLock has 3 modes of access: Read, Write, Optimistic Read.
• Read, Write: equivalent to ReadWriteLock while returning a stamp.
• Optimistic Read: doesn't prevent another thread from getting a write lock, which
will make the optimistic read lock stamp's invalid.
SPINLOCK
• A lock which causes a thread trying to acquire it to simply wait in a loop ("spin")
while repeatedly checking if the lock is available.
• Efficient if threads are likely to be blocked for only short periods.
SPINLOCK
SEMAPHORE
• Locks usually grant exclusive access to variables or resources.
• Semaphores are capable of maintaining whole sets of permits.
• Useful in scenarios where you have to limit the amount concurrent access to
certain parts of your application.
DISTRIBUTED LOCK
• Common problems : how to ensure that only one
process across a fleet of servers acts on a resource:
• processes may pause for arbitrary lengths of time
• packets may be arbitrarily delayed in the network
• clocks may be arbitrarily wrong
DISTRIBUTED LOCK
DISTRIBUTED LOCK
• Safety and Liveness guarantees
1. Safety property: Mutual exclusion. At any given moment, only one client can
hold a lock.
2. Liveness property A: Deadlock free. Eventually it is always possible to acquire
a lock, even if the client that locked a resource crashes or gets partitioned.
3. Liveness property B: Fault tolerance. As long as the majority of Redis nodes
are up, clients are able to acquire and release locks.
REDLOCK
• In order to acquire the lock, the client performs the following
operations:
• 1. Get the current time.
• 2. … All the steps needed to acquire the lock …
• 3. Get the current time, again.
• 4. Check if we are already out of time, or if we acquired the lock fast enough.
• 5. Do some work with your lock.
REDLOCK

More Related Content

Similar to Locks

An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in GoYu-Shuan Hsieh
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaLakshmi Narasimhan
 
Operating system 27 semaphores
Operating system 27 semaphoresOperating system 27 semaphores
Operating system 27 semaphoresVaibhav Khanna
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in JavaHome
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Bob Pusateri
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump AnalysisDmitry Buzdin
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaLuis Goldster
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHarry Potter
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaYoung Alista
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaTony Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaFraboni Ec
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHoang Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaJames Wong
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Bob Pusateri
 

Similar to Locks (20)

READER/WRITER SOLUTION
READER/WRITER SOLUTIONREADER/WRITER SOLUTION
READER/WRITER SOLUTION
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Java threading
Java threadingJava threading
Java threading
 
Operating system 27 semaphores
Operating system 27 semaphoresOperating system 27 semaphores
Operating system 27 semaphores
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 

Locks

  • 2. THREAD-SAFE • In multi-threaded programming, threads share resources. • It may lead to race condition. • A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads. • Synchronization is the most widely used method to achieve thread-safe. • Lock is a kind of synchronization.
  • 3. REENTRANTLOCK • A lock is acquired via lock() and released via unlock(). • If another thread has already acquired the lock, subsequent calls to lock() pause the current thread until the lock has been unlocked. • Only one thread can hold the lock at any given time. • tryLock() is an alternative to lock().
  • 4. READWRITELOCK • This type of lock maintains a pair of locks for read and write access. • Read-lock can be held simultaneously by multiple threads as long as no threads hold the write-lock. • Improve performance and throughput in situations that reads are more frequent than writes.
  • 6. STAMPEDLOCK • Also support read and write locks. • locking methods return a stamp. • These stamps are used to release a lock, to check if the lock is still valid or to convert the mode. • Main features of StampedLock compared to ReadWriteLock is mode conversion and optimistic read.
  • 7. STAMPEDLOCK MODE CONVERSION When and how to convert mode: • You acquired the write lock and wrote something and you want to read in the same critical section, use tryConvertToReadLock(long stamp). • You acquired the read lock, and after a successful read, you wanted to change the value, use tryConvertToWriteLock(long stamp).
  • 8. STAMPEDLOCK OPTIMISTIC READ StampedLock has 3 modes of access: Read, Write, Optimistic Read. • Read, Write: equivalent to ReadWriteLock while returning a stamp. • Optimistic Read: doesn't prevent another thread from getting a write lock, which will make the optimistic read lock stamp's invalid.
  • 9. SPINLOCK • A lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. • Efficient if threads are likely to be blocked for only short periods.
  • 11. SEMAPHORE • Locks usually grant exclusive access to variables or resources. • Semaphores are capable of maintaining whole sets of permits. • Useful in scenarios where you have to limit the amount concurrent access to certain parts of your application.
  • 12. DISTRIBUTED LOCK • Common problems : how to ensure that only one process across a fleet of servers acts on a resource: • processes may pause for arbitrary lengths of time • packets may be arbitrarily delayed in the network • clocks may be arbitrarily wrong
  • 14. DISTRIBUTED LOCK • Safety and Liveness guarantees 1. Safety property: Mutual exclusion. At any given moment, only one client can hold a lock. 2. Liveness property A: Deadlock free. Eventually it is always possible to acquire a lock, even if the client that locked a resource crashes or gets partitioned. 3. Liveness property B: Fault tolerance. As long as the majority of Redis nodes are up, clients are able to acquire and release locks.
  • 15. REDLOCK • In order to acquire the lock, the client performs the following operations: • 1. Get the current time. • 2. … All the steps needed to acquire the lock … • 3. Get the current time, again. • 4. Check if we are already out of time, or if we acquired the lock fast enough. • 5. Do some work with your lock.

Editor's Notes

  1. - Race condition: show example - Thread-safe: In particular, it must satisfy the need for multiple threads to access the same shared data, and the need for a shared piece of data to be accessed by only one thread at any given time.
  2. - Trylock: it tries to acquire the lock without pausing the current thread. - Reentrant means that locks are bound to the current thread. A thread can safely acquire the same lock multiple times without running into deadlocks.
  3. - The idea behind read-write locks is that it's usually safe to read mutable variables concurrently as long as nobody is writing to this variable.
  4. - The idea behind read-write locks is that it's usually safe to read mutable variables concurrently as long as nobody is writing to this variable.
  5. - ReadWriteLock is good, but it has some severe issues with starvations if not handled properly. For example, a number of reads but very few writes can cause the writer thread to fall into starvation. - Note: StampedLock is not reentrant, so each call to acquire the lock always returns a new stamp and blocks if there's no lock available, even if the same thread already holds a lock, which may lead to deadlock.
  6. - Note: Converting methods do not block and may return the stamp as zero, which means these methods' calls were not successful.
  7. - If using optimistic read, always check if the stamp is valid using the lock.validate(stamp) before reading.
  8. - Those instructions usually are much faster than synchronizing via locks. - Thread will access data from memory address and compare the current value with current expected value. If they match It will update the variable to new value. Obviously If multiple threads try making this operation simultaneously only one thread will succeed and others will fail. But other threads will not block instead invoker can continue with other operation or try the same again with failing threads. So this provides a lot better performance . 
  9. - A kind of busy waiting. - Because they avoid overhead from operating system process rescheduling or context switching.
  10. - A kind of busy waiting. - Because they avoid overhead from operating system process rescheduling or context switching.