From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 10 五月 2021 16:05:28 +0800
Subject: [PATCH] test lock code.

---
 src/robust.cpp |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/robust.cpp b/src/robust.cpp
index 08d2073..26d41b9 100644
--- a/src/robust.cpp
+++ b/src/robust.cpp
@@ -35,25 +35,24 @@
 
 bool FMutex::try_lock()
 {
-	if (flock(fd_, LOCK_EX | LOCK_NB) == 0) {
-		if (mtx_.try_lock()) {
+	if (mtx_.try_lock()) {
+		if (flock(fd_, LOCK_EX | LOCK_NB) == 0) {
 			return true;
 		} else {
-			flock(fd_, LOCK_UN);
+			mtx_.unlock();
 		}
 	}
 	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);
 	mtx_.lock();
+	flock(fd_, LOCK_EX);
 }
 void FMutex::unlock()
 {
-	mtx_.unlock();
 	flock(fd_, LOCK_UN);
+	mtx_.unlock();
 }
+
 } // namespace robust
\ No newline at end of file

--
Gitblit v1.8.0