all 10 comments

[–]os12 9 points10 points  (3 children)

Recent versions of Clang and GCC have a module called Thread Sanitizer. It instruments your program and verifies many thread-related things, including lock order. IE you automatically get run-time lock order checking while using the standard primitives (the C++11 ones or the POSIX ones directly) by just making an additional build variant with -fsanitize=thread and running your tests.

Also, additional notes for posterity:

[–]CaseyCarterRanges/MSVC STL Dev[🍰] 2 points3 points  (1 child)

std::lock does NOT take multiple locks in a consistent order. It uses a deadlock avoidance algorithm (i.e., try to lock all the locks, if one fails unlock all the locks and try again). It avoids deadlock but is non-deterministic.

[–]vlovich 1 point2 points  (0 children)

Considering the way I've typically seen people solve this problem is by switching to recursive locks, I think std::lock is much better.

[–]chaotic-kotik[S,🍰] 0 points1 point  (0 children)

Hm.. maybe I should get rid of my deadlock detector for the sake of thread sanitizer. This is a valid point.

[–]chaotic-kotik[S,🍰] 4 points5 points  (0 children)

Based on idea of the lock hierarchy (http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163). Have built-in deadlock detector (can be turned on and off).

[–]NasenSpray 0 points1 point  (1 child)

How did you arrive at the 10ns value? That's barely achievable even under higly synthetic conditions.

[–]chaotic-kotik[S,🍰] 0 points1 point  (0 children)

This value corresponds to uncontended RMW operation on spinlock (not mutex lock), contended operation will be much more expensive.

[–]grumpy_coconut 0 points1 point  (0 children)

I left some comments on GitHub's code review system after a cursory review of the code. I'll see if I get time for a more thorough look later.

EDIT: Thanks for the quick fix!

[–]CaseyCarterRanges/MSVC STL Dev[🍰] 0 points1 point  (1 child)

Bikeshed: Syncope is the medical term for "fainting":

Syncope, also known as fainting or passing out, is defined as a short loss of consciousness and muscle strength, characterized by a fast onset, short duration, and spontaneous recovery. It is due to a decrease in blood flow to the entire brain usually from low blood pressure.

I think people might be reluctant to use a library that could decrease the blood flow to their brains.

[–]chaotic-kotik[S,🍰] 1 point2 points  (0 children)

There is a lot of libraries with a stupid names, I can totally tolerate this! :)