From: Russell Hind (rhind_at_[hidden])
Date: 2003-07-18 02:55:03


This has probably been discussed before (most things to do with the
thread library have been) but I'll ask anyway:

Why does scoped_lock (and possibly the other lock) throw exceptions if
they are already locked upon calling lock?

     void lock()
     {
         if (m_locked) throw lock_error();
         lock_ops<Mutex>::lock(m_mutex);
         m_locked = true;
     }
     void unlock()
     {
         if (!m_locked) throw lock_error();
         lock_ops<Mutex>::unlock(m_mutex);
         m_locked = false;
     }

Surely if they are already locked, then this is a logic error on the
code side. I understand that mutex contructors and stuff throw if they
can't allocate the resources from the OS, as the code couldn't ensure
that would succeed, but if I call lock on an already locked scoped_lock,
then isn't that a coding error that could be caught with an assert
rather than an exception?

Thanks

Russell