
Frank Mori Hess wrote:
On Tuesday 15 January 2008 20:03, Phil Endecott wrote:
1. There's no "un-synchronised" version, for use in single-threaded programs or when all (first) accesses come from the same thread.
I took the bit in the documentation saying "implementing identical behavior in a single-threaded environment" to mean they are all un-synchronised, if you compile for example using gcc without the -pthread option.
Right, but in a large application I may have all uses of class X from a single thread, but still with other threads in completely different parts of the program (so I have to compile with -pthread).
2. The "mutexed access" feature can be useful in situation other than singletons; can't something more general-purpose be provided? I use a template class Lockable<T> which grants reader/writer-exclusive access to a contained T. (Reader/writer locking could be useful here.) (It might be useful to make the mutex type a template parameter, with a sensible default; I have a few mutexes of my own, with a boost::mutex-compatible interface.)
Is any of this available publically, or documentation for it available publically?
At http://svn.chezphil.org/libpbe/trunk/include/Locked.hh (note Locked<T>, not Lockable<T> as I mis-wrote above): // This provides a class template that augments a variable with a mutex, and // allows access only when the mutex is locked. Example: // // typedef Locked< vector<int> > x_t; // x_t x; // // // We can only modify x via an x_t::writer: // x_t::writer xw; // // Creating xw locks the mutex. // // xw behaves like a pointer to the underlying vector<int>: // xw->push_back(123); // xw->push_back(321); // // The lock is released when xw goes out of scope. // // To read, use an x_t::reader. It behaves like a const pointer to the // underlying data. // // The mutex and lock types can be specified with template parameters, but // they default to the boost versions. // // I had hoped to allow conversion-to-reference for the writer and reader, so // that you could write xw.push_back(123) rather than xw->push_back(123). // But this didn't work, presumably because I don't really understand how // implict type conversion is supposed to work. My attempt is still present, // commented out. It looks as if it hasn't done real reader/writer locking since that feature was removed from Boost. As for the mutexes, I suggest that you search for my name in this list's archive. This is GPL licensed. Let me know if you'd prefer something else. Regards, Phil.