Boost scoped_lock failed every time

人盡茶涼 提交于 2019-12-11 14:22:40

问题


In a class, I want to use a mutex over a function like this

void Agent::notify(Packet& packet, Peer peer) {
    boost::mutex::scoped_lock lock(mutex_);
    ...
}

No problem at the compilation process. But when I execute the program, boost always fail at this line saying :

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
Abandon (core dumped)

I try to use lock() or unlock methods, but the same problem. When I use try_lock it doesn't failed by the condition is always false.

Searching on internet I found this https://svn.boost.org/trac/boost/ticket/9307 .

But I think the problem is my program but I don't see where. In my tests there is only one thread that go in this function.


回答1:


The problem is that in the program the threads that use the mutex need to be at the same level. Yet the thread that created this error was the main thread. Not a created one.



来源:https://stackoverflow.com/questions/27660519/boost-scoped-lock-failed-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!