Release boost::mutex from destructor

2k views Asked by At

As std::vector isn't thread-safe, I was trying to build a very simple encapsulation around it which makes it thread-safe.

This works quite well, but there's one little problem. When the instance of the class is being destructed and another thread is still trying to read data out of it, the thread keeps hanging forever in the boost::mutex::scoped_lock lock(m_mutex);

How could I solve this? The best is to just unlock the mutex in order that the thread hanging in it can continue executing. I haven't defined a destructor because until now, it was not required.

Here my code. Note that there are more methods than shown here, it's been simplified.

template<class T>
class SafeVector 
{
    public:
    SafeVector();
    SafeVector(const SafeVector<T>& other);

    unsigned int size() const;
    bool empty() const;

    void clear();
    T& operator[] (const unsigned int& n);

    T& front();
    T& back();

    void push_back(const T& val);
    T pop_back();

    void erase(int i);

    typename std::vector<T>::const_iterator begin() const;
    typename std::vector<T>::const_iterator end() const;

    const SafeVector<T>& operator= (const SafeVector<T>& other);

    protected:
    mutable boost::mutex m_mutex;
    std::vector<T>  m_vector;

};

template<class T>
SafeVector<T>::SafeVector()
{

}

template<class T>
SafeVector<T>::SafeVector(const SafeVector<T>& other)
{
    this->m_vector = other.m_vector;
}

template<class T>
unsigned int SafeVector<T>::size() const
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.size();
}

template<class T>
bool SafeVector<T>::empty() const
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.empty();
}

template<class T>
void SafeVector<T>::clear()
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.clear();
}

template<class T>
T& SafeVector<T>::operator[] (const unsigned int& n)
{
    boost::mutex::scoped_lock lock(m_mutex);
    return (this->m_vector)[n];
}

template<class T>
T& SafeVector<T>::front()
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.front();
}

template<class T>
T& SafeVector<T>::back()
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.back();
}

template<class T>
void SafeVector<T>::push_back(const T& val)
{
    boost::mutex::scoped_lock lock(m_mutex);
    return this->m_vector.push_back(val);
}

template<class T>
T SafeVector<T>::pop_back()
{
    boost::mutex::scoped_lock lock(m_mutex);
    T back = m_vector.back();
    m_vector.pop_back();
    return back;
}

template<class T>
void SafeVector<T>::erase(int i)
{
    boost::mutex::scoped_lock lock(m_mutex);
    this->m_vector.erase(m_vector.begin() + i);
}

template<class T>
typename std::vector<T>::const_iterator SafeVector<T>::begin() const
{
    return m_vector.begin();
}

template<class T>
typename std::vector<T>::const_iterator SafeVector<T>::end() const
{
    return m_vector.end();
}

Edit I have to change my definition. The container clearly is not thread-safe as stated before. It isn't supposed to do so - even if the nomenclature is misleading. Sure you can do things with it that aren't thread-safe at all! But only one thread writes into the container, 2 or 3 read from it. It works well until I try to stop the process. I have to state that a monitor would have been better. But time's running out and I cannot change this until then.

Any idea is appreciated! Thanks and regards.

1

There are 1 answers

2
lefticus On BEST ANSWER

EDIT: Updated to be more complete example.

Others have pointed out the flaws with your "thread safety;" I'll attempt to answer your question.

The only proper way to do what you have set out to do is to make sure all of your threads have been shutdown before you try and destroy the vector itself.

A common method I have used is to simply use RAII to define the order of construction and destruction.

void doSomethingWithVector(SafeVector &t_vec)
{
  while (!boost::this_thread::interruption_requested())
  {
    //operate on t_vec
  }
}

class MyClassThatUsesThreadsAndStuff
{
  public:
    MyClassThatUsesThreadsAndStuff()
      : m_thread1(&doSomethingWithVector, boost::ref(m_vector)),
        m_thread2(&doSomethingWithVector, boost::ref(m_vector))
    {
      // RAII guarantees that the vector is created before the threads
    }

    ~MyClassThatUsesThreadsAndStuff()
    {
      m_thread1.interrupt();
      m_thread2.interrupt();
      m_thread1.join();
      m_thread2.join();
      // RAII guarantees that vector is freed after the threads are freed
    }

  private:
    SafeVector m_vector;
    boost::thread m_thread1;
    boost::thread m_thread2;
};

If you are looking for a more complete thread safe data structure that allows for multiple readers and writers, feel free to check out a queue I wrote using boost threads a while back.

http://code.google.com/p/crategameengine/source/browse/trunk/include/mvc/queue.hpp