gpt4 book ai didi

c++ - mutex lock fail with invalid argument 是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:21 66 4
gpt4 key购买 nike

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

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 failed everytime

更新:
这是 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);

关于c++ - mutex lock fail with invalid argument 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090108/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com