How can I take responsibility for the left boost :: interprocess :: interprocess_mutex?

My scenario: one server and some clients (although not many). A server can only respond to one client at a time, so they must be in the queue. I use the mutex ( boost::interprocess::interprocess_mutex ) for this, wrapped in boost::interprocess::scoped_lock .

The fact is that if one client dies unexpectedly (i.e. does not start the destructor) while saving the mutex, other clients have problems because they are waiting for this mutex. I examined the use of timeouts, so if a client waits for, say, 20 seconds and does not receive a mutex, he will continue negotiations with the server anyway.

Problems with this approach: 1) he does it every time. If he is in a cycle, constantly talking with the server, he needs to wait for a timeout every time. 2) If there are three clients, and one of them dies, holding the mutex, the other two will wait 20 seconds and simultaneously talk to the server - exactly what I tried to avoid.

So, how can I tell the client: "Hey, it looks like this mutex was abandoned, take responsibility for it"?

+7
c ++ synchronization boost concurrency boost-interprocess
source share
1 answer

Unfortunately, this is not supported by the boost :: interprocess as-is API. However, there are several ways to implement it:

If you are on a POSIX platform with pthread_mutexattr_setrobust_np support, edit boost / interprocess / sync / posix / thread_helpers.hpp and boost / interprocess / sync / posix / interprocess_mutex.hpp to use reliable mutexes and somehow call EOWNERDEAD to return from pthlock_m.

If you are on a different platform, you can edit boost / interprocess / sync / emulation / interprocess_mutex.hpp to use the generation counter, with the flag disabled in the low bit. You can then create a return protocol that sets a flag in the lock word to indicate a pending return, then do a comparison and swap after a timeout to verify that the same generation is still in the lock word, and if so, replace it with the next generation locked value.

If you are running Windows, another good option is to use your own mutex objects; they are likely to be more effective than anticipation anyway.

You may also need to rethink the use of the shared memory protocol - why not use the network protocol?

+6
source share

All Articles