I’m implementing a pair of classes for interprocess communication where one process will be the only writer and there will be many readers. One class handles reading; one handles writing. To prevent any other process from ever becoming the writer, I want a single object of the writer class which keeps an upgradable lock on a boost::named_upgradable_mutex for its entire lifetime. To that end, the writer class has a member variable of type boost::interprocess::upgradable_lock which is handed the mutex when the object is constructed. When it’s time for the writer process to write, it calls the writer class’s Write() method, which should upgrade that lock to be exclusive, perform the write, and atomically demote the exclusive lock to be merely upgradable again.
I’ve managed to implement the first part – upgrading the lock to be exclusive – in my writer class’s Write() method by following the Boost documentation on Lock Transfers Through Move Semantics. However, the second part – demoting the lock to be upgradable – results in a new local variable of type boost::interprocess::upgradable_lock which will go out of scope and release the mutex when Write() returns. I need to put that upgradable lock back in my class’s upgradable_lock member variable so the upgrade capability will remain solely in my writer object. What’s the best way to do this? The only thing I’ve managed to come up with is to swap the local variable with my member variable before returning. The code looks like this:
using boost::interprocess;
scoped_lock<named_upgradable_mutex> my_exclusive_lock(move(m_lock));
// do write here
upgradable_lock<named_upgradable_mutex> my_demoted_lock(move(my_exclusive_lock));
m_lock.swap(my_demoted_lock); // how else to do this?
This works, but that last line was really counterintuitive and took me a while to think of. Is there a better way? Is it possible to put the demoted lock directly into my member variable? Also, are there any unintended consequences of reusing the member variable to store the demoted lock?
Consider using the move assignment operator. The following code using
swap():would become:
In this particular case,
swap()and the move assignment operator are interchangeable without any side effects becausem_lockis in the default constructed state (m_lock.owns() == falseandm_lock.mutex() == 0).I cannot think of any unintended consequences of reusing the member variable for the upgradeable lock. However, there are a few topics to consider:
One goal is “to prevent any other process from ever becoming the writer”. With the lock being acquired in the
Writerconstructor, the code is preventing other processes from creating aWriter, in addition to preventing other processes from writing. As a result, the blocking call may be imposing or inconvenient to application code. Consider the following code:A compromising alternative may be to try to acquire the lock via this constructor, then add member functions to
Writerthat provide the application more control. While this would still allow other processes to create aWriter, it prevents more than one from having the privilege to write:Within the
Writer::Write()method, if any of the calls in the “do write here” code throw an exception, then the stack will unwind, resulting in:my_exclusive_lockreleasing the exclusive lock, allowing other processes to obtain the upgradeable lock.m_locknot having a handle to the mutex, asm_lock.mutex()is set tonullwhen ownership was transferred tomy_exclusive_lockin the move.Writer::Write()would attempt to write without acquiring the exclusive lock! Even ifm_lockhad a handle to the mutex,m_lock.owns()would befalse, so a transfer tomy_exclusive_lockwould not attempt to lock.Here is an example program:
Which produces the following output: