#include <filelock.h>
Guard to create a file lock.
This can be used to make sure concurrent processes do not interfere when for example writing files.
The lock file will be deleted when the FileLock is destroyed.
Internally this uses some POSIX system calls to atomically check for the existence of a file and create it if it does not exists. This works even over NFS (for at least NFSv3 and Linux 2.6).
Definition at line 30 of file filelock.h.
Public Member Functions | |
FileLock (const std::filesystem::path &path) | |
Construct a file lock guard with a lock file at the given path. More... | |
~FileLock () | |
Delete the lock file when the guard is destroyed. More... | |
bool | acquire () |
Try to acquire the file lock. More... | |
Private Attributes | |
std::filesystem::path | path_ |
Path to the file lock. More... | |
bool | acquired_ |
Whether the lock has been acquired. More... | |
|
explicit |
Construct a file lock guard with a lock file at the given path.
This will not create a file, use acquire() for that.
[in] | path | Path to file. |
Definition at line 19 of file filelock.cc.
smash::FileLock::~FileLock | ( | ) |
Delete the lock file when the guard is destroyed.
Definition at line 41 of file filelock.cc.
bool smash::FileLock::acquire | ( | ) |
Try to acquire the file lock.
std::runtime_error | if called another time after returning true or if the lockfile cannot be closed. |
Definition at line 22 of file filelock.cc.
|
private |
Path to the file lock.
Definition at line 54 of file filelock.h.
|
private |
Whether the lock has been acquired.
Definition at line 56 of file filelock.h.