
Anthony Williams schrieb:
I'll see if I can word things better. Thanks!
Could you show the code for Lock and Condition? Also, does this happen on Windows, a pthreads platform or both? Happens on Windows, not tested on pthreads. I only tested on windows as I have been using custom condition/threads there.
My lock is: class Lock : private boost::noncopyable { public: Lock (Mutex& m); bool locked () const; private: std::tr1::shared_ptrdetail::LockImpl lock_; friend class Condition; }; with class LockImpl { public: boost::mutex::scoped_lock lock_; LockImpl::LockImpl (MutexImpl& m) : lock_ (m.m_) { } bool LockImpl::locked () const { return lock_; } }; and the Lock constructor does: Lock::Lock (Mutex& m) : lock_ (new LockImpl (*m.m_)) { } condition being: class Condition : private boost::noncopyable { public: Condition (); // Just pass through void notifyAll (); void notifyOne (); // See below void wait (Lock& lock); private: std::tr1::shared_ptrdetail::ConditionPrivate impl_; }; with class ConditionPrivate { public: // Just pass through void notifyAll (); void notifyOne (); // See below void wait (LockImpl& lock); private: boost::condition cond_; }; and here is the interesting part, I just extract the scoped_lock and pass it on. void ConditionPrivate::wait (detail::LockImpl& lock) { cond_.wait (lock.lock_); } Condition::Condition () : impl_ (new ConditionPrivate ()) { } void Condition::wait (Lock& lock) { impl_->wait (*lock.lock_); } and finally: class Mutex : private boost::noncopyable { public: Mutex (); ~Mutex (); void lock (); bool tryLock (); void release (); private: std::tr1::shared_ptrdetail::MutexImpl mutex_; friend class Lock; }; which passes through to class MutexImpl { public: boost::mutex m_; void MutexImpl::lock () { m_.lock (); } void MutexImpl::release () { m_.unlock (); } bool MutexImpl::tryLock () { return m_.try_lock (); } }; These are just simple wrappers, to get around the problem that windows.h gets included by date_time and to reduce compile times. Moreover, I had the old implementation (Win32 only) working just like this. Calling a lock via another function shouldn't matter as it just makes the call slower but not less thread safe (as the object does get locked eventually, and from that point on, the execution is serialized), or am I misunderstanding something? Thanks for taking a look, Anteru