每次升压scoped_lock失败 [英] Boost scoped_lock failed every time

查看:138
本文介绍了每次升压scoped_lock失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个类中,我想在这样的函数上使用互斥锁

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_);
    ...
}

在编译过程中没有问题.但是,当我执行程序时,boost总是在此行失败:

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)

我尝试使用lock()unlock方法,但是存在相同的问题.当我使用try_lock时,它不会失败,条件始终为false.

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.

在互联网上搜索时,我发现了这个 https://svn.boost.org/trac/boost/ticket/9307 .

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.

推荐答案

问题是,在程序中,使用互斥锁的线程必须处于同一级别.但是,导致此错误的线程是主线程.不是创建的.

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.

这篇关于每次升压scoped_lock失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆