Overview
Features
Download
Documentation
Community
Add-Ons & Services

what is wrong with this usage of mutex?

Please post support and help requests here.

what is wrong with this usage of mutex?

Postby dagg » 11 Jun 2009, 21:53

I get the following compilation errors:
Code: Select all
error: passing ‘const Poco::Mutex’ as ‘this’ argument of ‘void Poco::Mutex::lock()’ discards qualifiers
error: passing ‘const Poco::Mutex’ as ‘this’ argument of ‘void Poco::Mutex::unlock()’ discards qualifiers

from the following code:
Code: Select all
template <typename T>
const bool Queue<T>::empty() const {
   mLock.lock();
   bool ans=mQueue.empty();
   mLock.unlock();
   return ans;
}

where mLock is declared:
Code: Select all
private:
      Poco::Mutex mLock;


what is wrong with that implementation?
dagg
 
Posts: 18
Joined: 28 Mar 2008, 09:07

Re: what is wrong with this usage of mutex?

Postby guenter » 11 Jun 2009, 22:00

Your member function is const, and as such cannot modify the mutex object (what Mutex::lock() does).
Solution: declare your Mutex member as mutable, as in:
Code: Select all
private:
    mutable Poco::Mutex mLock;


I'd also suggest using ScopedLock, as in:
Code: Select all
template <typename T>
const bool Queue<T>::empty() const {
   Poco::Mutex::ScopedLock lock(mLock);
   return mQueue.empty();
}
guenter
 
Posts: 1110
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: what is wrong with this usage of mutex?

Postby WittrockJ » 11 Jun 2009, 22:05

You are trying to modify the state of the member mLock from a const method.
Try making mLock mutable.

You might also consider using a Poco::ScopedLock instead of calling lock() and unlock().

-Wittrock

Edit
Oops. Looks like Guenter beat me to it.
WittrockJ
 
Posts: 5
Joined: 07 May 2009, 16:15

Re: what is wrong with this usage of mutex?

Postby dagg » 12 Jun 2009, 06:29

thanks. the mutable made the difference.
dagg
 
Posts: 18
Joined: 28 Mar 2008, 09:07


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron