Question

Is there any standard threading library for Linux which implements hybrid mutex capabilities? I mean, a mutex which, at first, "behaves like a spinlock" and "only if the lock has still not been obtained after a certain amount of time (or retries or any other measuring factor), the thread is really put to sleep", as mentioned by Mecky at When should one use a spinlock instead of mutex?

Was it helpful?

Solution

The glibc already provides this. Just use the PTHREAD_MUTEX_ADAPTIVE_NP type or use PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP as the mutex initializer.

See http://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_mutex_lock.c#l90

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top