Middle+
What is an advisory lock?
sobes.tech AI
Answer from AI
Advisory lock — this is a locking mechanism that is not enforced automatically by the system but is used based on an agreement between processes. That is, the system does not prohibit access to the resource, but processes that use advisory lock voluntarily check and adhere to the lock.
In Go (and other languages), advisory lock is often implemented through system calls, such as flock or fcntl on Unix systems.
Features:
- Locking only works if all processes agree to use it.
- It does not block access at the OS level but only signals to other processes that the resource is busy.
Example of using advisory lock in Go:
import (
"os"
"syscall"
)
func lockFile(file *os.File) error {
return syscall.Flock(int(file.Fd()), syscall.LOCK_EX)
}
func unlockFile(file *os.File) error {
return syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
}
Thus, advisory lock is useful for coordinating access between processes that agree to use this mechanism.