Boost logo

Boost Users :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2008-01-10 11:47:39


Srinivas Gardas escribió:
> Hi,
> I am a little confused about how to make 'interprocess_mutex' part of the
> 'managed_shared_memory' object which actually represents the shared_memory?
> Is there a base class or something that can take the mutex?
>
> I placed an '*interprocess_mutex*' as a member variable *in the class that
> contains my 'managed_shared_memory'* object as a member variable and was
> locking it in the methods that accessed the shared memory. I guess this did
> not work out as I believe what I did effectively locked the object of my
> class that was holding the embers. And since this is not actually the shaed
> resource between the processes it didn't work out..
>
> I'd really appreciate if you could provide an example of just show me bbased
> on my initial code.. how the 'managed_shared_memory' can be locked using the
> mutex.
>
> Sreenu

Just create the mutex like you create the list and use scoped_lock to
lock and unlock it more easily (pseudo_code, not compiled):

using namespace boost::interprocess;

interprocess_mutex *mut = m_shMemPtr->find_or_construct
<interprocess_mutext>("PROCESS-SHARED MUTEX")();

struct_a stOne;
strcpy(stOne.sym, "ABCDE");
strcpy(stOne.desc, "FGHIJK");
stOne.price = 10.25;

{ //Begin of locked scope

   //Now lock the mutex to protect concurrent access
   //to the list
   scoped_lock<interprocess_mutex> locker(*mut);

   //Do the operation
   m_pList->push_back(stOne);

   //Mutex will be unlocked by locker's destructor
} //End of locked scope

The other process should also lock the same mutex when operating with
the list so that one process is blocked while the other one is
manipulating the list. There are several alternatives to build the mutex
in the segment but I think this is the simplest one.

Regards,

Ion


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