So I once learned that UNIX futex is in essence an unique integer, is this statement correct and complete?
If that is true, can I somehow take advantage of it to help avoid deadlock by always locking multiple ones in an ascending/descending order?
I guess this idea somewhat resembles the hierarchical_mutex introduced in A. William's book "C++ Concurrency in Action", where the author uses some metadata combined with std::mutex to achieve that.
A futex is an integer, but not a unique one. The value of the integer represents the state of the futex (i.e. acquired or not acquired) and changes throughout its lifetime.
The identity of the futex resides in its memory location, not its value. That is, two futexes are different if they live at different memory locations.
Unfortunately we cannot use memory addresses to establish an order between futexes in a multiprocess situation. A futex can reside in shared memory and a block of shared memory can be mapped anywhere. It could easily happen that block A is mapped above block B in process X but below block B in process Y.
So there is no reliable built-in order you can use to help avoid deadlocks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With