I added a Mutex class to bind to RAII. I am not sure if I am using it correctly.
After the queue is blocked by the manufacturer, the program unexpectedly quits.
Mutexclass.h
#ifndef MUTEXCLASS #define MUTEXCLASS #include <pthread.h> class MutexClass { private: pthread_mutex_t & _mutexVariable; public: MutexClass (pthread_mutex_t &); ~MutexClass (); }; #endif // MUTEXCLASS
Mutexclass.cpp
#include "mutexClass.h" #include <stdexcept> MutexClass::MutexClass (pthread_mutex_t & arg) : _mutexVariable (arg) { _mutexVariable = PTHREAD_MUTEX_INITIALIZER; int returnValue = pthread_mutex_lock (&_mutexVariable); if (returnValue > 0) { throw std::logic_error ("Mutex couldn't be locked!"); } } MutexClass::~MutexClass() { pthread_mutex_unlock (&_mutexVariable); }
This is main.cpp where I use an object of the above mutex class.
The Qt classes here are for the namesake, because I am a Qt Creator. Please ignore them.
#include "mainwindow.h"
The conclusion is here:
... ... Queue 140388157085440 Removed by Consumer: 1403881570854401403881654781442 Queue , Locked by Consumer: 1140388148692736 Removed by Consumer: , Length of queue 1 is: , First check by Producer: 140388148692736, Length of queue 2 is: 1403881654781449 Queue Queue 2, UnLocked by Consumer: 140388148692736 Consumer: 9 Queue 1, UnLocked by Consumer: 140388157085440 Queue 2, First check by Consumer: 1403881570854401140388148692736 Queue 1, First check by Consumer: 140388148692736 Queue , Locked by Producer: Queue 2, Locked by Consumer: 140388157085440 Removed by Consumer: 1403881654781441, Locked by Consumer: 140388148692736 Pushed by Producer 140388165478144: Length of queue 1 is: 10 Queue 1, UnLocked by Producer: 140388165478144 Queue 2, First check by Producer: 140388165478144 Queue 2, Locked by Producer: 140388165478144The program has unexpectedly finished.
Also pay attention to the following part of the output:
Queue 2 blocked by user: 140388157085440 Deleted by user: 1403881654781441, Blocked by User: 140388148692736
I created only 2 users, but the numbers shown here are 3. Why is this so?