I was thinking of the following scenario happening while a pthread is running:
pthread_mutex_lock(...);
... // <- Thread dies here
pthread_mutex_unlock(...);
in other words, a pthread starts, at some point it locks a mutex, and for some reason, it dies before it is able to call the matching unlock function, either because another thread killed it, or the OS itself (android in this case), decided it needed to kill it for some reason or other (it needed to do other things, etc.)
Would this ever happen on android (or any other OS really) without you invoking a pthread_kill() or something like that, and even if you did, what is the proper way to avoid just such a thing from happening? Thank you.
Killing threads is never very useful. (unless you can afford to SIGKILL/abort the whole process anyway).
Instead unwind the stack with an exception and use RAII. If your process/OS has become so unstable that random thread aborts happen, I think you’ll have other worries and the resulting mess is not the process’ responsibility.
Just Don’t
pthread_killAlso, all of this might become slightly more interesting when using inter-process sync primitives. In that case, though, I think Linux kernels guarantee that any locks held by a process will be released when that process is terminated, whatever the cause