Windows CRITICAL_SECTION is implemented in a way lighter than mutex ( as far as for the lock/unlock perf cost ) By utilizing a spinlock and a semaphore to reduce user/kernel switches.
A mutex require a context switch even if the mutex is not currently owned/locked where a CRITICAL_SECTION will req that only after a period of time sinning on the spin lock ( results in better perf ).
I am new to Android Native dev, having in mind windows CRITICAL_SECTION, is there an equivalent in Native Android?
Is there anything lighter than pthread_mutex_. @ Android? does ‘pthread_mutex_.‘ impose a context switch even if the mutex is not already ~owned/locked~ ( as in windows )?
Having in mind rapid entering/exiting a critical section What is the usage cost of ‘pthread_mutex_.‘ impose ?
Is there a user-mode spinlock @ Native Android ?
Any help will be appreciated.
Nadav at Sophin
No, the
pthread_mutex_lock()in Android’s Bionic libc does not impose a context switch for locking a normal mutex in the uncontended state – a simple atomic compare-and-exchange is used, followed by a memory barrier. Likewise unlocking a mutex does not require a kernel entry if there is no process waiting on it.You can find the Bionic implementation of
pthread_mutex_lock()andpthread_mutex_unlock()inlibc/bionic/pthread.cin the Bionic libc sources and see this yourself.In general you can consider
pthread_mutex_lock()/pthread_mutex_unlock()to be quite lightweight.