开发者

removing a boost named_mutex

I have the following code:

void Func()
{开发者_如何学运维
    boost::interprocess::named_mutex someMutex(boost::interprocess::open_or_create, "MyMutex");
    boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(someMutex);

    // ... some stuff happens here
}

and many applications are calling Func. If there is a crash in Func, the mutex isn't released, so I'm considering removing the mutex, by calling boost::interprocess::named_mutex::remove("MyMutex"), if I detect any of my app crashed.

Due to special circumstance, it's actually safe for two threads or processes to enter the protected area at the same time, because the content of Func() only does things for the first application that runs.

I have two questions:

  1. is what I'm planning to do a good idea?
  2. What happens if:
    • Process A opens the mutex and locks it (the mutex had been created previously)
    • Process B dies. We detect that and remove the mutex
    • Process C creates the mutex and locks it
    • Process A finishes running Func(), and the scoped_lock destructor releases the mutex
    • Process C finishes running Func(), and the scoped_lock destructor releases the mutex

now do I have a "double released" named_mutex, or did Process A just didn't do anything in the scoped_lock destructor because the named_mutex it locked had already been removed?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜