Mutex-protected objects

#include <x/ref.H>
#include <x/mpobj.H>

class msgObj;

// A container for pending messages

typedef std::list<x::ref<msgObj> > messages_t;

// Pending messages

x::mpcobj<messages_t> messages;

// A lock on the messages queue

typedef x::mpcobj<messages_t>::lock lock_t;

// ...

   lock_t lock(messages);


// ...

x::ref<msgObj> msg=({
   lock_t lock(messages);

   while (lock->empty())

   auto msg=lock->front();

The x::mpcobj template class implements a design pattern for a mutex-protected object, which attaches a std::mutex and a std::condition_variable. Access to the object requires obtaining a lock, which then may be used as a pointer to the locked object. The lock also provides access to the underlying condition variable, for signaling and waiting purposes.


These are not reference-counted classes. The underlying mutex-protected object instance must remain in scope as long as there are instances of instantiated locks.

An x::mpobj template class implements a mutex-protected object without a condition variable. Use it when a condition variable is not needed, only an object that's protected by a mutex.

The second optional parameter to the x::mpobj or the x::mpcobj template overrides std::mutex as the underlying mutex type.