Boost logo

Boost Users :

Subject: Re: [Boost-users] [interprocess] shared memory, condtions, deadlock
From: Kai Benndorf (kai.benndorf_at_[hidden])
Date: 2010-08-05 08:03:54


Just for the curious,

>I'm trying to use the shared memory functionality of the boost
>interprocess library for implementing a client and a server like
>process, where the server calculates something for the client. I use
>condition variables for the process synchronization. The >implementation
>works initially, but I am getting deadlocks, now.

in the meanwhile i found a solution on my own. The lock on the mutex
must be made directly at the beginning of each process, so that the
mutex is only unlocked if wait() is called.

Regards

Kai

Something like this works:

class SharedData
{
   // Excuted by the client process
   void calc() {
     scoped_lock<interprocess_mutex> lock( itsMutex );
     while( somethingToCalc ) {
       // prepare data

       itsPrepared.notify_one();
       itsCalced.wait( lock );

       // process results
     }
   }

   // Excuted by the server process
   void serverCalc() {
     scoped_lock<interprocess_mutex> lock( itsMutex);
     while( somethingToCalc ) {
       itsPrepared.wait( lock );

       // Calc

       itsRhsCalced.notify_one();
     }
   }

private:
   boost::interprocess::interprocess_mutex itsMutex;

   boost::interprocess::interprocess_condition itsPrepared;

   boost::interprocess::interprocess_condition itsCalced;

   // some further data
};


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