Will killed process/thread release mutex?
Asked Answered
B

1

11

Several processes access shared memory, locking it with the mutex and pthread_mutex_lock() for synchronization, and each process can be killed at any moment (in fact I described php-fpm with APC extension, but it doesn't matter).
Will the mutex be unlocked automatically, if the process locked the mutex and then was killed?
Or is there a way to unlock it automatically?

Edit: As it turns out, dying processes and threads have similar behavior in this situation, which depends on robust attribute of mutex.

Babettebabeuf answered 29/9, 2014 at 16:48 Comment(6)
It depends on the type of mutex being used, which you didn't specify. Punch robust mutex into your favorite search engine.Grobe
Please see this related question and as David said look into robust mutexes. They don't get unlocked after a crash, but the next thread that will attempt to lock it will receive a EOWNERDEAD error code.Renewal
It would be nice to make an answer out of that, but David's comment is probably too terse to be an answer as-is.Siouxie
I'd consider this question being a duplicate to the question linked by Cicada's comment. It's been answered by you there? @R..Tazza
@DavidSchwartz, thank you, robust mutexes is what I looked for.Babettebabeuf
@Cicada, I thought, that dying processes and threads should behave differently, so I didn't read this question. Thank you!Babettebabeuf
E
2

That depends on the type of mutex. A "robust" mutex will survive the death of the thread/process. See this question: POSIX thread exit/crash/exception-crash while holding mutex

The next thread that will attempt to lock it will receive a EOWNERDEAD error code

Note: Collected information from the comments.

Electrolysis answered 30/9, 2014 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.