How to access mutex created by a C++ process in a Python process?

Viewed 228

I'm planning on using boost::interprocess to share memory between C++ and Python processes. Presuming that I'll need a mutex provided by boost to make sure only one process gets to access memory, how do I get python to acknowledge and unlock/lock the mutex created by boost?

1 Answers

There seem to be two obvious approaches:

  • Find an existing python wrapper for boost
  • Write a C++ Python extension module that provides a domain-specific API for your application

Using the example in the link above and the some guesswork, you'd have something like this:

   static PyObject *mySharedMutex_lock(PyObject *self, PyObject *args)
   {
       const char *objectName;
       int sts;

       if (!PyArg_ParseTuple(args, "s", &objectName))
       {
           return NULL;
       }
       boost::interprocess::named_mutex mutex(open_or_create, objectName);
       mutex.lock();
       return Py_None;
   }

Obviously you'd need the other boiler-plate from the instructions at the link above, and to probably provide a way of unlocking the mutex. Doesn't look very onerous to get this working.

Related