具有无效参数的互斥锁失败是什么意思? [英] What does mutex lock fail with invalid argument mean?
问题描述
此代码在我的主进程中被调用并且可以正常编译,但是在执行时始终会在下面引发错误.
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
'class bounded_buffer'的代码完全与此增强示例页面上显示的一样...
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
我在这里找到此页面,该页面似乎显示了完全相同的内容,但是我无法理解给出的答案. Boost scoped_lock每次都失败
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
更新:
这是调用函子时Producer :: operator()当前所做的事情.而我对这个线程想要做什么的意图.
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)
}*/
}
推荐答案
该函数返回并且 bb
被销毁,但线程仍在运行.而当 m_container
尝试使用互斥锁时,它(连同整个 m_container
一起)将不复存在.
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();
或者您需要将数据的所有权传递给线程,例如.使用 std :: shared_ptr
(如果您想像Boost示例中那样与 Consumer
共享缓冲区,但与示例中未加入线程的情况不同):
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);
这篇关于具有无效参数的互斥锁失败是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!