std::thread::hardware_concurrency(); std::lock(); std::recursive_mutex;
Rules to follow:
OPTION 1: PASS IN A REFERENCE
OPTION 2: REQUIRE A NO-THROW COPY CONSTRUCTOR OR MOVE CONSTRUCTOR
OPTION 3: RETURN A POINTER TO THE POPPED ITEM
OPTION 4: PROVIDE BOTH OPTIONS 1 AND EITHER OPTION 2 OR 3.
class X { private: some_big_object some_detail; mutable std::mutex m; public: X(some_big_object const& sd):some_detail(sd){} friend bool operator<(X const& lhs, X const& rhs) { if(&lhs==&rhs) return false; std::lock(lhs.m,rhs.m); #1 std::lock_guard<std::mutex> lock_a(lhs.m,std::adopt_lock); #2 std::lock_guard<std::mutex> lock_b(rhs.m,std::adopt_lock); #3 return lhs.some_detail<rhs.some_detail; } };
Guidelines for Avoiding Deadlock:
AVOID NESTED LOCKS
AVOID CALLING USER-SUPPLIED CODE WHILST HOLDING A LOCK
ACQUIRE LOCKS IN A FIXED ORDER
USE A LOCK HIERARCHY
EXTENDING THESE GUIDELINES BEYOND LOCKS
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.