NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
file_locker类 参考

文件区域锁管理类 更多...

#include <file_locker.hpp>

Public 类型

using native_handle_type = _NEFORCE native_handle_type
 原生文件句柄类型
using difference_type = int64_t
 偏移量类型

Public 成员函数

 file_locker (native_handle_type handle) noexcept
 构造函数
bool lock (difference_type offset, difference_type length, file_lock mode=file_lock::EXCLUSIVE) const noexcept
 锁定文件区域
bool unlock (difference_type offset, difference_type length) const noexcept
 解锁文件区域
bool try_lock (difference_type offset, difference_type length, file_lock mode) const noexcept
 尝试锁定
NEFORCE_NODISCARD bool is_locked (difference_type offset, difference_type length, file_lock *lock_out=nullptr) const noexcept
 查询区域是否被锁定
bool lock_whole (file_lock mode=file_lock::EXCLUSIVE) const noexcept
 锁定整个文件
bool unlock_whole () const noexcept
 解锁整个文件

详细描述

文件区域锁管理类

对文件句柄提供区域级的锁定与解锁操作:

  • 多进程间同步文件访问
  • 保护文件的关键区域
  • 实现进程间互斥
注解
文件锁是劝告性的(advisory),不强制遵守。进程可以选择忽略锁状态继续访问文件。
不持有文件句柄所有权,句柄生命周期由调用方保证。

在文件 file_locker.hpp41 行定义.

构造及析构函数说明

◆ file_locker()

file_locker::file_locker ( native_handle_type handle)
explicitnoexcept

构造函数

参数
handle已打开的文件句柄

关联指定的文件句柄。

引用了 file_locker() , 以及 handle().

被这些函数引用 file_locker().

成员函数说明

◆ is_locked()

NEFORCE_NODISCARD bool file_locker::is_locked ( difference_type offset,
difference_type length,
file_lock * lock_out = nullptr ) const
noexcept

查询区域是否被锁定

参数
offset起始偏移量
length查询长度
lock_out如果非空,输出当前持有的锁类型
返回
区域被锁定返回true,未被锁定返回false

查询指定区域是否被其他进程锁定。

注解
由于锁状态随时变化,查询结果仅作参考。

◆ lock()

bool file_locker::lock ( difference_type offset,
difference_type length,
file_lock mode = file_lock::EXCLUSIVE ) const
noexcept

锁定文件区域

参数
offset起始偏移量(字节)
length锁定长度(字节),0表示到文件末尾
mode锁定模式
返回
锁定成功返回true,失败返回false

阻塞等待直到获得锁。 如果length为0,锁定从offset到文件末尾的所有区域。

引用了 EXCLUSIVE.

◆ lock_whole()

bool file_locker::lock_whole ( file_lock mode = file_lock::EXCLUSIVE) const
noexcept

锁定整个文件

参数
mode锁定模式
返回
锁定成功返回true

锁定从文件开头到末尾的整个文件。

引用了 lock_whole().

被这些函数引用 lock_whole().

◆ try_lock()

bool file_locker::try_lock ( difference_type offset,
difference_type length,
file_lock mode ) const
noexcept

尝试锁定

参数
offset起始偏移量
length锁定长度
mode锁定模式
返回
立即获得锁返回true,锁被占用返回false

尝试获取锁,如果锁已被占用则立即返回false,不会阻塞等待。

◆ unlock()

bool file_locker::unlock ( difference_type offset,
difference_type length ) const
noexcept

解锁文件区域

参数
offset起始偏移量
length解锁长度,0表示到文件末尾
返回
解锁成功返回true,失败返回false

释放指定区域的锁。解锁的区域必须与锁定的区域完全匹配。

◆ unlock_whole()

bool file_locker::unlock_whole ( ) const
noexcept

解锁整个文件

返回
解锁成功返回true

解锁整个文件。

引用了 unlock_whole().

被这些函数引用 unlock_whole().


该类的文档由以下文件生成: