Probably the most portable locking mechanism other than C ++ 11 is the synchronization of the Boost.Thread library . In particular, the mutex class gives you a simple lockable object to provide exclusive access to a resource. For instance:
#include <boost/thread/mutex.hpp> #include <queue> template <typename T> class locking_queue { public: void push(T const & value) { boost::mutex::scoped_lock lock(mutex); queue.push(value); } bool pop(T & value) { boost::mutex::scoped_lock lock(mutex); if (queue.empty()) { return false; } else { value = queue.front(); queue.pop(); return true; } } private: std::queue<T> queue; boost::mutex mutex; };
Another advantage is that it is very similar to the C ++ 11 std::mutex
, which will make the conversion pretty simple if you decide to use it instead.
source share