1

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'

Community
  • 1
  • 1
Navin
  • 554
  • 2
  • 9
  • 31

2 Answers2

3

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

badger the bold
  • 421
  • 2
  • 4
  • This is a good explanation, but i am not getting few thing over and over again. Every where i read they are stating what you mentioned " the compiler generated copy constructor tries to copy the mutex, but it's copy constructer is private. hence the error", i don't get. in C# and Java this is not a problem, because MessageQueue and Request are 2 different classes so how the same Mutex could be copied? – Navin Aug 10 '12 at 06:31
  • 1
    I think the actual problem is when inserting Request into the map. This copies the request. It is clear to me now, that the alternative I gave you is what you need to use. p.s. Visual C++ errors regarding compiler generated constructors is usually not very helpful in pinpointing where the actual error is located. – badger the bold Aug 10 '12 at 06:56
  • Your correct that is where the problem is. As you said Visual C++ errors arnt that clear. I'll look in to it. – Navin Aug 10 '12 at 09:05
1

It would be nice if you posted the line on which you are getting this compile error. But I strongly suspect you are trying to call the copy constructor of the boost::mutex class which (the constructor) is private.

Alexander Chertov
  • 2,070
  • 13
  • 16