Boost logo

Boost Users :

From: Anteru (newsgroups_at_[hidden])
Date: 2008-04-08 16:27:45


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_ptr<detail::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_ptr<detail::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_ptr<detail::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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net