删除boost :: interprocess :: named_mutex的问题

时间:2014-02-04 07:21:02

标签: c++ boost boost-interprocess

我在下面的程序中做了,但最后没能删除named_mutex,打印出“互斥删除失败”的结果

void IPC::testNamedMutex()
{
named_mutex mutex(open_or_create, "MyMutex");
for (int i = 0; i < 10; i++)
{
    mutex.lock();
    cout << "Mutex taken" << endl;

    std::fstream fs("test.txt", std::fstream::out | std::fstream::app);
    if (fs)
    {
        fs << "Thread id: " << boost::this_thread::get_id() << ", "
                << "Iteration " << i << endl;
    }

    boost::this_thread::sleep(boost::posix_time::seconds(1));
    mutex.unlock();
    cout << "Mutex is unlocked" << endl;

}
cout << "Delete the file and mutex?(y/n): ";
char c;
cin >> c;
if (c == 'y' || c == 'Y')
{
    if (remove("test.txt"))
        cout << "File deleted" << endl;
    else
        cout << "File delete failed" << endl;

    bool success=named_mutex::remove("MyMutex");
    if (success)
        cout << "Mutex removed" << endl;
    else
        cout << "Mutex delete failure" << endl;
}
}

但是,如果我使用下面的第二个程序运行删除,它可以工作。可能是什么原因?

void IPC::testDeleteNamedMutex()
{
cout << named_mutex::remove("MyMutex") << endl;
}

1 个答案:

答案 0 :(得分:0)

AFAIK如果在高架环境中运行时创建了互斥锁,则会在Windows上发生这种情况,但您尝试将其作为普通用户删除。

换句话说,UAC再次破坏了事情。

理论上,您应该能够设置ACL(访问控制列表)以向目标用户/组授予权限。我没试过这个。如果需要,我会咨询Technet和/或您当地的管理员以了解更多信息。