繁体   English   中英

什么是互斥锁失败,无效参数意味着什么?

[英]What does mutex lock fail with invalid argument mean?

此代码在我的主进程中调用并编译正常,但执行时总是抛出下面的错误。

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

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

这是执行时总是出现的错误。

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'的代码完全如此boost示例页面所示... http://www.boost.org/doc/libs/1_55_0/libs/circular_buffer/example/circular_buffer_bound_example.cpp

我在这里找到了这个页面似乎显示完全相同的东西,但我无法理解给出的答案。 Boost scoped_lock每次都失败了

更新:
这是调用仿函数时Producer :: operator()当前的作用。 我的意图是我想要这个线程做的事情。

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 )不再存在。

您需要等待线程结束,然后才能销毁它使用的任何数据:

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

或者您需要将数据的所有权传递给线程,例如。 使用std::shared_ptr (如果你想在Boost示例中与Consumer共享缓冲区,但与不加入线程的示例不同):

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM