Boost logo

Boost Users :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2008-05-20 10:06:30


Jason Robertson wrote:
> Hi,
> I'm trying to create simple locks based on a string name. I looked at the interprocess
>module which has a named mutex,but it seems not well suited/efficient
for use in a single
> process, though I know it's supposed to work.

I don't recommend using Interprocess named mutex. It has different
lifetime semantics than intra-process named mutexes and it can be less
efficient than other mutexes.

I don't know about the Named Mutex pattern, but this pattern seems a bit
resource expensive (creating and destroying mutexes on the fly).
Wouldn't be better just to store the mutexes in the map without seeing
if a client has destroyed it and re-create it again?

Regards,

Ion

> Instead, I'm just creating a map<string,weak_ptr<mutex>> in a NamedMutexManager class.
>It's used as a singleton and uses brute force locking to implement a
named lock pattern.
> Main method is
> shared_ptr<mutex> GetMutex(string name) {
> BigLock (only one thread at a time can get a lock so we synchronize access to map)
> check map
> if we have entry, make sure weak_ptr hasn't been reclaimed
> If so, make a new shared_ptr, save weak_ptr by name, return shared_ptr
> If not, return shared_ptr copy from weak_ptr.lock()
> If we habe no entry,
> make a new shared_ptr, save weak_ptr by name, return shared_ptr
> }
> I'm just getting back to C++ after years, so I'm 99% sure there's probably a much better way.
> Is there a better way to do an intra-process, reliable mutex by
std::string name so I can
> lock things by name? How are other people doing this, do you find
> interprocess:named_mutex suitable for use in-process?
> I had problems with it.
> Thanks!
> Jason


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net