Apart from the fact that you do not need to throw from the constructor in your specific case because pthread_mutex_lock
actually returns an EINVAL if your mutex has not been initialized and you can throw after the call to lock
as is done in std::mutex
:
void
lock()
{
int __e = __gthread_mutex_lock(&_M_mutex);
// EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
if (__e)
__throw_system_error(__e);
}
then in general throwing from constructors is ok for acquisition errors during construction, and in compliance with RAII ( Resource-acquisition-is-Initialization ) programming paradigm.
Check this example on RAII
void write_to_file (const std::string & message) {
// mutex to protect file access (shared across threads)
static std::mutex mutex;
// lock mutex before accessing file
std::lock_guard<std::mutex> lock(mutex);
// try to open file
std::ofstream file("example.txt");
if (!file.is_open())
throw std::runtime_error("unable to open file");
// write message to file
file << message << std::endl;
// file will be closed 1st when leaving scope (regardless of exception)
// mutex will be unlocked 2nd (from lock destructor) when leaving
// scope (regardless of exception)
}
Focus on these statements:
static std::mutex mutex
std::lock_guard<std::mutex> lock(mutex);
std::ofstream file("example.txt");
The first statement is RAII and noexcept
. In (2) it is clear that RAII is applied on lock_guard
and it actually can throw
, whereas in (3) ofstream
seems not to be RAII , since the objects state has to be checked by calling is_open()
that checks the failbit
flag.
At first glance it seems that it is undecided on what it the standard way and in the first case std::mutex
does not throw in initialization , *in contrast to OP implementation * . In the second case it will throw whatever is thrown from std::mutex::lock
, and in the third there is no throw at all.
Notice the differences:
(1) Can be declared static, and will actually be declared as a member variable
(2) Will never actually be expected to be declared as a member variable
(3) Is expected to be declared as a member variable, and the underlying resource may not always be available.
All these forms are RAII; to resolve this, one must analyse RAII.
- Resource : your object
- Acquisition ( allocation ) : you object being created
- Initialization : your object is in its invariant state
This does not require you to initialize and connect everything on construction. For example when you would create a network client object you would not actually connect it to the server upon creation, since it is a slow operation with failures. You would instead write a connect
function to do just that. On the other hand you could create the buffers or just set its state.
Therefore, your issue boils down to defining your initial state. If in your case your initial state is mutex must be initialized then you should throw from the constructor. In contrast it is just fine not to initialize then ( as is done in std::mutex
), and define your invariant state as mutex is created . At any rate the invariant is not compromized necessarily by the state of its member object, since the mutex_
object mutates between locked
and unlocked
through the Mutex
public methods Mutex::lock()
and Mutex::unlock()
.
class Mutex {
private:
int e;
pthread_mutex_t mutex_;
public:
Mutex(): e(0) {
e = pthread_mutex_init(&mutex_);
}
void lock() {
e = pthread_mutex_lock(&mutex_);
if( e == EINVAL )
{
throw MutexInitException();
}
else (e ) {
throw MutexLockException();
}
}
// ... the rest of your class
};
std::lock_guard
for a similar implementation. – Inflexedlock
andunlock
so that your mutex type works withstd::lock_guard
; he's reimplementingstd::mutex
, notstd::lock_guard
here, and there is a reason the two classes are separate in the C++ standard library. – Jabez