Boost logo

Boost Users :

Subject: Re: [Boost-users] Assertion `!pthread_mutex_destroy(&m)' failed.
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-03-25 16:04:54


AMDG

On 03/25/2011 12:58 PM, Panagiotis Foteinos wrote:
> I am new to boost:threads, so my apologies in advance.
>
> I am getting an assertion failure right before main() exits. Specifically, I
> get the following error: "*/usr/include/boost/thread/pthread/mutex.hpp:45:
> boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' failed.*"
>
> Below is the code snippet:
>
> ***********************************************************************************************************************************
> class Test
> {
> private:
> boost::mutex mtx;
>
> public:
> int counter;
> Test()
> {
> counter = 10;
> }
> void DoWork()
> {
> for(unsigned int i = 0; i< 1000000; i++)
> {
> mtx.try_lock();
> counter++;
> mtx.unlock();
> }
> }
> };
>
>
> int main()
> {
> unsigned int N = 7;
> boost::thread* test_threads = new thread_type[N];
> for(unsigned int i = 0; i< N; i++)
> {
> test_threads[i] =
> thread_type(boost::lambda::bind<void>(&Test::insert,&object));
> }
>
> for(unsigned int i = 0; i< N; i++)
> {
> test_threads[i].join();
> }
>
> return 1;
> }
> ***********************************************************************************************************************************
>
>
> Can somebody shed some light? Main() exits normally, if I replace
> mtx.try_lock() with mtx.lock(). Why this behavior?
>

You have to test the result of try_lock. try_lock
doesn't block if the mutex is already locked, thus
your code can call unlock on a mutex that isn't locked.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net