i am using boost mutex in MessageQueue class as a private member in the following method
void MessageQueue::Dequeuee()
{
Request rq(messageWareHouse.front().reqID,messageWareHouse.front().seq,
messageWareHouse.front().sMessage);
while(true)
{
boost::unique_lock<boost::mutex> lock(qMutex);
qCond.wait(lock);
**reqCollection**.find(messageWareHouse.front().reqID)->second.addSegments(messageWareHouse.front().seq,
messageWareHouse.front().sMessage );
}
....
reqCollection is a map
map<size_t, Request> reqCollection;
Inside Request when i try to initialize the mutex i am getting the below error
class Request
{
private:
size_t RequestID;
public:
boost::mutex qMutex;
Request(size_t requestID,size_t seq, std::string sMessage);
void addSegments(size_t seq, std::string sMessage);
};
as far as i searched this error in google here the solution for the problem is stated as
Place (smart) pointers for the mutex or the class containing the mutex
but does this mean i can only use one mutex variable in my whole project by passing pointers? Why boost is protecting the mutex
the error is
Error 7 error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex'
The mutex can't be copied, it is a system resource, so if you have it in some class which get's copied, the compiler generated copy constructor is called, the compiler generated copy constructor tries to copy the mutex, but it's copy constructer is private. hence the error.
If you wan't a copy of the resource chances are you'de like a different mutex, so you would need to write you're own copy constructor. If you wan't the copied objects to share the mutex you can use shared_ptr.
Alternatively you might not want to copy the objects at all, then you can use
std::map< size_t, std::unique_ptr< Request > >
if you don't have std::unique_ptr use shared_ptr