From cf05ea3d9f43e4e84d621e1f9d54cbef552b6e2b Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 18 五月 2021 16:53:28 +0800 Subject: [PATCH] fix center init mutex. --- src/robust.cpp | 13 +++++++++---- 1 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/robust.cpp b/src/robust.cpp index 08d2073..4654652 100644 --- a/src/robust.cpp +++ b/src/robust.cpp @@ -36,24 +36,29 @@ bool FMutex::try_lock() { if (flock(fd_, LOCK_EX | LOCK_NB) == 0) { + ++count_; if (mtx_.try_lock()) { return true; } else { - flock(fd_, LOCK_UN); + if (--count_ == 0) { + flock(fd_, LOCK_UN); + } } } return false; } void FMutex::lock() { - //Note: the lock order affects performance a lot, - // locking fd_ first is about 100 times faster than locking mtx_ first. flock(fd_, LOCK_EX); + ++count_; mtx_.lock(); } void FMutex::unlock() { mtx_.unlock(); - flock(fd_, LOCK_UN); + if (--count_ == 0) { + flock(fd_, LOCK_UN); + } } + } // namespace robust \ No newline at end of file -- Gitblit v1.8.0