问题
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