
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.
Instead, I'm just creating a map

Jason Robertson
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. Instead, I'm just creating a map
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
A common way to do named mutexes is with a hash table. In the absence of a true concurrent hash table, you can use a fixed number of buckets, with a mutex on each. You then hash the name to choose a bucket, lock the appropriate mutex and search the bucket for the entry, adding a new one if not there. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

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
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

Windows has it built it (I guess it comes from OpenVMS)
HANDLE WINAPI CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName
);
OpenVMS has a small data part on the locks in which a user can store
some data. This is another form of IPC. Wish POSIX supported this
because at times I would like to know from where the lock came to me
or who owns the lock I am trying to acquire.
-dhruva
On Tue, May 20, 2008 at 7:36 PM, Ion Gaztañaga
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
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Contents reflect my personal views only!
participants (4)
-
anthony_w.geo@yahoo.com
-
dhruva
-
Ion Gaztañaga
-
Jason Robertson