views:

381

answers:

3

I have a custom class that uses boost mutexes and locks like this (only relevant parts):

template<class T> class FFTBuf
{
    public:
        FFTBuf(); 
        [...]
        void lock();
        void unlock();
    private:
        T *_dst;
        int _siglen;
        int _processed_sums;
        int _expected_sums;
        int _assigned_sources;
        bool _written;
        boost::recursive_mutex _mut;
        boost::unique_lock<boost::recursive_mutex> _lock;
};

template<class T> FFTBuf<T>::FFTBuf() : _dst(NULL), _siglen(0),
    _expected_sums(1), _processed_sums(0), _assigned_sources(0),
    _written(false), _lock(_mut, boost::defer_lock_t())
{
}

template<class T> void FFTBuf<T>::lock()
{
    std::cerr << "Locking" << std::endl;
    _lock.lock();
    std::cerr << "Locked" << std::endl;
}

template<class T> void FFTBuf<T>::unlock()
{
    std::cerr << "Unlocking" << std::endl;
    _lock.unlock();
}

If I try to lock more than once the object from the same thread, I get an exception (lock_error):

#include "fft_buf.hpp"

int main( void ) {
    FFTBuf<int> b( 256 );
    b.lock();
    b.lock();
    b.unlock();
    b.unlock();

    return 0;
}

This is the output:

sb@dex $ ./src/test
Locking
Locked
Locking
terminate called after throwing an instance of 'boost::lock_error'
   what(): boost::lock_error
zsh: abort    ./src/test

Why is this happening? Am I understanding some concept incorrectly?

+2  A: 

Try this:

template<class T> void FFTBuf<T>::lock()
{
    std::cerr << "Locking" << std::endl;
     _mut.lock();
    std::cerr << "Locked" << std::endl;
}

template<class T> void FFTBuf<T>::unlock()
{
    std::cerr << "Unlocking" << std::endl;
    _mut.unlock();
}

You use the same instance of unique_lock _lock twice and this is a problem. You either have to directly use methods lock () and unock() of the recursive mutex or use two different instances of unique_lock like foe example _lock and _lock_2;.

Update

I would like to add that your class has public methods lock() and unlock() and from my point of view in a real program it is a bad idea. Also having unique_lock as a member of class in a real program must be often a bad idea.

skwllsp
This works, but why?
Kjir
Then what is the use for boost::unique_lock? What advantage does it give over simply using the mutex?
Kjir
RAII, I guess. http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
skwllsp
+2  A: 

The lock should not be part of the protected ressource but of the caller as you have one caller by thread. They must use different unique_lock.

The purpose of unique_lock is to lock and release the mutex with RAII, so you don't have to call unlock explicitly.

When the unique_lock is declared inside a method body, it will belong to the calling thread stack.

So a more correct use is :

#include <boost/thread/recursive_mutex.hpp>
#include <iostream>

template<class T>
class FFTBuf
{
public :
    FFTBuf()
    {
    }

    // this can be called by any thread
    void exemple() const
    {
        boost::recursive_mutex::scoped_lock lock( mut );
        std::cerr << "Locked" << std::endl;

        // we are safe here
        std::cout << "exemple" << std::endl ;

        std::cerr << "Unlocking ( by RAII)" << std::endl;
    }

    // this is mutable to allow lock of const FFTBuf
    mutable boost::recursive_mutex mut;
};    

int main( void )
{
    FFTBuf< int > b ;

    {
        boost::recursive_mutex::scoped_lock lock1( b.mut );
        std::cerr << "Locking 1" << std::endl;

        // here the mutex is locked 1 times

        {
            boost::recursive_mutex::scoped_lock lock2( b.mut );
            std::cerr << "Locking 2" << std::endl;

            // here the mutex is locked 2 times

            std::cerr << "Auto UnLocking 2 ( by RAII) " << std::endl;
        }

        b.exemple();

        // here the mutex is locked 1 times

        std::cerr << "Auto UnLocking 1 ( by RAII) " << std::endl;
    }

    return 0;
}

Note the mutable on the mutex for const methods.

And the boost mutex types have a scoped_lock typedef which is the good unique_lock type.

fa.
+3  A: 

As the name implies, the Mutex is recursive but the Lock is not.

That said, you have here a design problem. The locking operations would be better off not being accessible from the outside.

class SynchronizedInt
{
public:
  explicit SynchronizedInt(int i = 0): mData(i) {}

  int get() const
  {
    lock_type lock(mMutex);
    toolbox::ignore_unused_variable_warning(lock);

    return mData;
  }

  void set(int i)
  {
    lock_type lock(mMutex);
    toolbox::ignore_unused_variable_warning(lock);

    mData = i;
  }


private:
  typedef boost::recursive_mutex mutex_type;
  typedef boost::unique_lock<mutex_type> lock_type;

  int mData;
  mutable mutex_type mMutex;
};

The main point of the recursive_mutex is to allow chain locking in a given thread which may occur if you have complex operations that call each others in some case.

For example, let's add tweak get:

int SynchronizedInt::UnitializedValue = -1;

int SynchronizedInt::get() const
{
  lock_type lock(mMutex);
  if (mData == UnitializedValue) this->fetchFromCache();
  return mData;
}

void SynchronizedInt::fetchFromCache()
{
  this->set(this->fetchFromCacheImpl());
}

Where is the problem here ?

  • get acquires the lock on mMutex
  • it calls fetchFromCache which calls set
  • set attempts to acquire the lock...

If we did not have a recursive_mutex, this would fail.

Matthieu M.
The problem is that I have a function which returns a pointer and I need ensure synchronizations on operations done outside my class. So this is why I need the lock and unlock methods, I put them inside the class to avoid the need to explicitly use boost::*_lock in the outer code. I should probably think of a better design...
Kjir
There is always the solution where you return a ref counted owner to the lock to guarantee the destruction.
Matthieu M.