What needs to be corrected, added, or subtracted here?
class mutexLocker
{
private:
/* Declaration of a Mutex variable `mutexA`. */
pthread_mutex_t &mutexA;
/* `mutexStatus` holds the return value of the function`pthread_mutex_lock `.
This value has to be returned to the callee so we need to preserve it in a class
variable. */
int mutexStatus;
public:
/* Constructor attempts to lock the desired mutex variable. */
mutexLocker (pthread_mutex_t argMutexVariable)
: mutexA (argMutexVariable)
{
/* Return value is needed in order to know whether the mutex has been
successfully locked or not. */
int mutexStatus = pthread_mutex_lock (&argMutexVariable);
}
/* Since the constructor can't return anything, we need to have a separate function
which returns the status of the lock. */
int getMutexLockStatus ()
{
return mutexStatus;
}
/* We may need this Mutex variable as an argument for `pthread_cond_wait()`*/
pthread_mutex_t getLockedMutex ()
{
if (mutexStatus >= 0)
return mutexA;
}
/* The destructor will get called automatically whereever the callee's scope ends, and
will get the mutex unlocked. */
~mutexLocker ()
{
if (mutexStatus >= 0)
pthread_mutex_unlock (&mutexA);
}
};
