我正在尝试使用
file_lock
用于限制同一程序同时运行的多个实例(实现中提到的内容
this answer
). 我正在使用
1.66
boost版本
Linux
.
在锁定文件之前,我确保该文件存在(通过使用以下命令打开它
std::ofstream
具有
std::ios::app
). 我注意到一件事,如果我们关闭溪流,那么
file_lock
自动解锁,因此允许同一程序的多个实例同时运行。
当file_lock被自动释放时,下面的程序不起作用。
int main(int argc, char *argv[])
{
namespace bipc = boost::interprocess;
if (argc < 2)
return 0;
std::string path = argv[1];
std::string lock_path = "/var/lock/" + path + ".lock";
std::ofstream stream(lock_path, std::ios::app);
bipc::file_lock lock(lock_path.c_str());
if (!lock.try_lock())
throw std::runtime_error("Multiple instance");
std::cout << "Running" << std::endl;
stream.close();
while (true)
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}
然而,下面的两个程序是有效的。
看起来,如果我们在尝试获取file_lock时打开了一个文件,那么我们需要保持该文件打开,直到我们想要持有锁为止。如果我们关闭文件,则锁会自动释放。我不确定这是不是一个bug。有人能帮我解释一下这种行为的原因吗?
int main(int argc, char *argv[])
{
namespace bipc = boost::interprocess;
if (argc < 2)
return 0;
std::string path = argv[1];
std::string lock_path = "/var/lock/" + path + ".lock";
std::ofstream stream(lock_path, std::ios::app);
bipc::file_lock lock(lock_path.c_str());
if (!lock.try_lock())
throw std::runtime_error("Multiple instance");
std::cout << "Running" << std::endl;
while (true)
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}
还有
int main(int argc, char *argv[])
{
namespace bipc = boost::interprocess;
if (argc < 2)
return 0;
std::string path = argv[1];
std::string lock_path = "/var/lock/" + path + ".lock";
{
std::ofstream stream(lock_path, std::ios::app);
}
bipc::file_lock lock(lock_path.c_str());
if (!lock.try_lock())
throw std::runtime_error("Multiple instance");
std::cout << "Running" << std::endl;
while (true)
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}