![](/img/trans.png)
[英]what does lock_guard always owns the lock mode of the referenced mutex mean?
[英]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.