Archived

This topic is now archived and is closed to further replies.

fov

C++ const member function problem

Recommended Posts

Dear everybody, I recently implemented a linked list class wich protects access with a mutex (so i can use it safely from multiple threads). Now since I use a mutex, there appeared compiler errors on const member functions that use the mutex, e.g:
unsigned int CMyList::Count () const
{
    m_Mutex.Enter();
    // count elements and store number in nCount
    m_Mutex.Leave();
    return nCount;
}
 
Now when compiling this code, the compiler (gcc as well as vc) gives me: passing ''const CMyMutex'' as ''this'' argument of ''void CMyMutex::Enter()'' discards qualifiers. And it''s right... Within a const member function, everything of the class is const and one may not modify it. But CMyMutex::Enter() is not const and so the compiler gives me an error. But I think this error is unnecessary. Yes, the mutex object is modified by Enter() and Leave(), but this will not change the list content itself. It''s only modified temporary to prevent changes from other threads during Count(). Now I have 2 ideas to solve this problem, but none of them satisfies me: 1) I could make CMyMutex::Enter() and CMyMutex::Leave() become const member functions. This would probably fix the problem, but it''s not the right way (I think), because even if it''s meaningless to CMyList, the mutex object itself is changed by Enter() and Leave(). 2) I could make CMyList::Count() (and all other affected functions) become non-const member functions. This would also fix the compiler error, but would require many other functions to be changed because they rely on Count() to be const. In fact I need a way to have CMyList::Count() as a const member function which is able to temporarly enter/leave the mutex object. Is there a C++ guru out there who knows what''s the ''right way'' to reach this? I''d appreciate any help. Thanks in advance, fov

Share this post


Link to post
Share on other sites
Make m_Mutex a "mutable" member of CMyList.

Then you can call non-const methods of m_Mutex and the
compiler won't complain.


  
class CMyList
{
mutable CMutex m_Mutex;
// Blah Blah

};
// end


unsigned int CMyList::Count () const
{
m_Mutex.Enter();
// count elements and store number in nCount

m_Mutex.Leave();
return nCount;
}
// end



I'd suggest that you use a "lock" in conjunction with the
mutex. This way, you won't get a deadlock if an exception
is thrown in between Enter() and Leave(). I know counting
the elements probably won't throw exceptions, but for other
things, they may throw exceptions.

~~~~
Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on July 15, 2002 6:33:51 PM]

Share this post


Link to post
Share on other sites
Lucky for you, there is a C++ keyword the will handle this for you. The keyword is "mutable". Put this keyword in front of your definition to m_Mutex and it should solve your problem. It''s in MSDN if you want the exact definition of it.


Merowe

Share this post


Link to post
Share on other sites