[英]i got 'boost::fibers::lock_error' and can't figure out why
i run this code 我运行这段代码
class ttt {
public:
~ttt() {
LOG(INFO);
flush();
}
bool flush() {
//std::lock_guard<boost::fibers::mutex> lock(_mutex);
LOG(INFO);
_mutex.lock();
LOG(INFO);
auto ret = flush_nonlock();
LOG(INFO);
_mutex.unlock();
LOG(INFO);
return ret;
}
private:
bool flush_nonlock() {
LOG(INFO);
return std::rand()%2;
}
boost::fibers::mutex _mutex;
};
int main() {
static ttt t;
std::cout << t.flush() << std::endl;
return 0;
}
and i got 我得到了
terminate called after throwing an instance of 'boost::fibers::lock_error'
what(): boost fiber: a deadlock is detected: Resource deadlock avoided
the last log it print is before the _mutex.lock(). 它打印的最后一个日志在_mutex.lock()之前。 if t is not a static variable, it won't throw any error.
如果t不是静态变量,则不会引发任何错误。 if i remove t.flush() in main func, it won't throw any error.
如果我在主函数中删除t.flush(),则不会引发任何错误。 use std::lock_guard as i wrote in notes, the line next to it is not printed.
使用std :: lock_guard,就像我在笔记中所写的那样,它旁边的行未打印。 i can't figure out why and whats the diff about the cases i tried.
我不知道为什么,我尝试过的案件有何不同。
i build the code use gcc 5.4.0, with -O0 我用-O0构建代码使用gcc 5.4.0
static ttt t;
your static instance of ttt in main might be destructed after the internal data of boost.fiber have been destructed. 在boost.fiber的内部数据被销毁后,可能会销毁main中的ttt静态实例。 Accesing
context::active()
in mutext::lock()
from ~ttt()
might return a null-pointer. Accesing
context::active()
在mutext::lock()
从~ttt()
可能会返回一个空指针。
edit: boost.fiber uses internally a thread-local static holding the active fiber (in order to enable suspending from a deep call stack). 编辑:boost.fiber在内部使用一个线程局部静态变量来保存活动光纤(以便能够从深度调用堆栈中挂起)。 because ttl is declared static, the compiler can destruct the ttl instance and the boost.fiber internall static in arbitrary order.
因为ttl被声明为静态的,所以编译器可以按任意顺序破坏ttl实例和boost.fiber内部的static。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.