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(). if t is not a static variable, it won't throw any error. if i remove t.flush() in main func, it won't throw any error. use std::lock_guard as i wrote in notes, the line next to it is not printed. 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