Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does mutex lock fail with invalid argument mean?

Tags:

c++

boost

This code is called in my main process and compiles fine, but when executed always throws the error below.

bounded_buffer<MyData> bb(200);
Producer<bounded_buffer<MyData> > producer(&bb);

boost::thread produce(producer); // throws on this line

Here is the error that always appears when executing.

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'  
what():  boost: mutex lock failed in pthread_mutex_lock: Invalid argument

The code for 'class bounded_buffer' is exactly as shown on this boost example page ... http://www.boost.org/doc/libs/1_55_0/libs/circular_buffer/example/circular_buffer_bound_example.cpp

I found this page here which seems to show the exact same thing, but I was unable to understand the answer given. Boost scoped_lock failed everytime

Update:
Here is what Producer::operator() currently does when the functor is called. And my intentions for what I want this thread to do.

void operator() () {
    //init();
    //read();

    // this line just a test
    m_container->push_front(value_type());

    /*Eventually will do the following:
    while (1) {
         read_from_usb_device();
         // then store data in global buffer (bb)
    }*/
}
like image 321
Ender Avatar asked May 07 '15 00:05

Ender


People also ask

What happens when mutex lock?

Mutexes are used to protect shared resources. If the mutex is already locked by another thread, the thread waits for the mutex to become available. The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it.

What is mutex lock in C++?

The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. mutex offers exclusive, non-recursive ownership semantics: A calling thread owns a mutex from the time that it successfully calls either lock or try_lock until it calls unlock .

Is mutex lock blocking?

mutex::lock Locks the mutex. If another thread has already locked the mutex, a call to lock will block execution until the lock is acquired. If lock is called by a thread that already owns the mutex , the behavior is undefined: for example, the program may deadlock.

Can a mutex be locked twice?

If a mutex is locked twice by a thread, the result is undefined, it might throw an error or the thread could be blocked forever.


1 Answers

The function returns and bb gets destroyed but the thread is still running. And when m_container tries to use the mutex, it (along with the whole m_container) no longer exists.

You need to wait for the thread to end before you can destroy any data it uses:

boost::thread produce(producer);
produce.join();

Or you need to pass the ownership of the data to the thread, eg. using std::shared_ptr (if you want to share the buffer with Consumer as in the Boost example but unlike the example not joining the threads):

auto bb = std::make_shared<bounded_buffer<MyData> >(200);
Producer<bounded_buffer<MyData> > producer(bb);
Consumer<bounded_buffer<MyData> > consumer(bb);
boost::thread produce(producer);
boost::thread consume(consumer);
like image 78
StenSoft Avatar answered Sep 17 '22 22:09

StenSoft