我遇到了同样的问题,路径仅包含使用 boost 1.65.1 的目录:
unexpected exception: boost::filesystem::weakly_canonical: The process cannot access the file because it is being used by another process;
当路径包含符号链接时,这种情况也仅发生在网络驱动器上。
看来这是一个同步问题。显然使用boost:filesystem无法并行访问相同的符号链接。
我定义了一个自定义函数,用于封装并同步对weakly_canonical的访问:
static boost::recursive_mutex sgCanonicalMutex;
boost::filesystem::path CanonicalPath(const boost::filesystem::path inPath)
{
boost::recursive_mutex::scoped_lock lk(sgCanonicalMutex);
return boost::filesystem::weakly_canonical(inPath);
}
更改后,该问题不再出现。
boost::filesystem::status 的文档中还有关于底层系统错误代码 ERROR_SHARING_VIOLATION 的注释。看https://www.boost.org/doc/libs/1_70_0/libs/filesystem/doc/reference.html https://www.boost.org/doc/libs/1_70_0/libs/filesystem/doc/reference.html
我认为根本原因在于升压源:
boost\libs\filesystem\src\operations.cpp
函数 read_symlink 包含
handle_wrapper h(
create_file_handle(p.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, 0));
第三个参数(值 0)是传递给 CreateFileW 的 dwShareMode(请参阅https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew)。
该参数可能应该是 FILE_SHARE_READ。最新的升压1.70仍然保持不变。